Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Element Missing in output XML file?
by roboticus (Chancellor) on Mar 11, 2008 at 11:41 UTC
    Anonymous Monk:

    I'm not certain I understand your question, and I've never used XML::LibXML, so I may be wildly off base. That said, a cursory examination suggests that your problem is with this line:

     for my $para ($document->getElementsByTagName ("p")){

    I suspect that getElementsByTagName doesn't find any paragraph elements for the cases indicated, so your code skips the entire for loop body. It's hard to be sure, though, as you forgot to show us some test data to check against.

    ...roboticus

      Thank you for your reply.

      But I wonder why I get part of the output missing.....by the way, can you suggest any stable XML creator?

        Again ... you don't provide any sample data for input, and your code isn't complete, so I don't have much to go on.

        Looking again at your output, it appears that you're probably executing the original code fragment in a loop over a set of files. If so, I'd suspect that 0.xml has a "p" element in it, while 10.xml doesn't....

        As regards to an XML creator: Sorry--I don't use it, so I couldn't advise anyone about good packages to use.

        A super search on that topic would probably give you some good leads, though.

        ...roboticus