Here is the other part of the code that follows the xml file example.
#!/usr/bin/perl use XML::Parser; @files = <$plrepository/*.xml>; foreach $xmlfile (@files) { #something is omitted $p2 = new XML::Parser(Handlers => {Start => \&handle_start, End => \&handle_end, Char => \&handle_char}); $p2->parsefile($xmlfile); } sub handle_start { my ($pkg,$element,%attr) = @_; $current_element = $element; if ( $element =~ /Header/i ) { $Number=$attr{Number}; open (OUT, ">$outputfile") or die "No file"; } elsif ( $element =~ /ContentElement/i ) { $IdNumber=$attr{IdNumber}; $InstanceNumber=$attr{InstanceNumber}; } $Number=''; # I empty the variable for the next Number in the fil +e $IdNumber=''; $InstanceNumber=''; #something is omitted } sub handle_end { my ($pkg,$element,%attr) = @_; if ( $element =~ /Header/i ) { print OUT $Number,"$separator\n"; print "\tNumber ". $Number . "\n"; close (OUT); } elsif ( $element =~ /ContentElement/i ) { print OUT $IdNumber,"$separator\n"; print "\tIDNumber ". $IdNumber . "\n"; print OUT $InstanceNumber,"$separator\n"; print "\tIstanceNumber ". $InstanceNumber . "\n"; + close (OUT); } $Number=''; # I empty the variable for the next Number in the fil +e $IdNumber=''; $InstanceNumber=''; #something is omitted } sub handle_char { my $text = $_[1]; if ( $current_element =~ /^Number$/i ) { ($text !~ /^\s*$/) && ($Number .= $text); #|-> buffer text } if ( $current_element =~ /^IdNumber$/i ) { ($text !~ /^\s*$/) && ($IdNumber .= $text); #|-> buffer text } if ( $current_element =~ /IstanceNumber/i ) { ($text !~ /^\s*$/) && ($IstanceNumber .= $text); #|-> buffer text } #something is omitted }
I have to print out different CSV files like this:
AC_1234, yyyyyyyy-yy,01463010000016 zzzzzzzz-zz,0000000000000000 xxxxxxxx-xx,111111111111111 aaaaaaaaa-aa,222222222222222
but without buffering i have something like this:
AC_1234, yyyyyyyy-yy,01463010000016 zzzzzzzz-zz,0000000000000000 xxx-xx,111111111111111 aaaaaaaaa-aa,22222
and buffering the text, sometimes happen this:
AC_1234, yyyyyyyy-yy,01463010000016 zzzzzzzz-zz,0000000000000000 xxxxxxxx-xx,111111111111111 xxxxxxxx-xxaaaaaaaaa-aa,222222222222222111111111111111
I hope that now something is much more clear. B/R

In reply to Re^4: Another problem with XML parser by Anonymous Monk
in thread Another problem with XML parser by Paulux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.