Hi monks, I am trying to writing log file in xml with XML::LibXML 1.5x after I do some processing, but something string occur: some element is missing in final xml:
my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($file); $root = $doc->documentElement(); $cdoc = $doc->getElementsById($docname); open(FH, ">", $file) or die "Open file '$tracefile' failed: $!\n"; flock(FH, LOCK_SH); seek(FH, 0, SEEK_SET); for my $para ($document->getElementsByTagName ("p")){ my $status = processing($para); my @paras = $root->getElementsByTagName('sentence'); my $exist_para = $root->getElementsById($para->getAttribute("i +d")); my $para_elem; if(!defined($exist_para)){ $para_elem = $root->createElement('sentence'); $para_elem->setAttribute('xml:id', $para->getAttribute("id +")); $para_elem->setAttribute('index', scalar(@paras)); }else{ $para_elem = $exist_para; } if($status == 1){ $para_elem->setAttribute('class', 'problem'); }elsif($status == 0){ $para_elem->setAttribute('class', 'fine'); }else{ $para_elem->setAttribute('class', 'skip'); } $cdoc->appendChild($para_elem); }
Result (partial):
<document src="0.xml" xml:id="0.xml" index="0"> <sentence xml:id="0_xml0002" index="0"/> </document> <document src="10.xml" xml:id="10.xml" index="1"> </document> <document src="16.xml" xml:id="16.xml" index="3"> <sentence xml:id="16xml_0008" index="2"/> </document>
The index here shown I get some trouble that the element is not written out while I am processing (seems instead of write out the element string, it print a newline only)....and they are really missing when I compare the log on the screen. It appear in both case that I write the file at once or parse/write repeatedly. Any Idea?

In reply to Element Missing in output XML file? by Anonymous Monk

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.