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

Hi all,

Update: On the advice given i have reduced the sample code size.

This is a really simple problem so should not take long to fix. Its down to my incompetance and inability to code XML (having never done it before, though its like HTML ;| )

Im currently using XML::Writer if that helps

I have output that says

<?xml version="1.0"?> <Main> <NewHits> </NewHits> <OldHits> </OldHits> <MatchedHits> </MatchedHits> </Main>
yet i have this code to fill in the tags with data: i know its repeated but i will fix that later on.
sub writeXML() # HTML code { my $output = new IO::File(">comOutput/$newRefFile.xml"); my $writer = new XML::Writer(OUTPUT => $output); $writer->xmlDecl(); $writer->startTag("Main"); $writer->startTag("NewHits"); # write only the new hits to the file foreach my $element(@newHits) { $writer->startTag( "GI" ); $writer->dataElement( "GI" , $element); $writer->endTag( "GI" ); $writer->startTag("E-value"); $writer->dataElement( "E-value" , $evals{$element}); $writer->endTag("E-value"); $writer->startTag("Score"); $writer->dataElement( "Score" , $newScores{$element}); $writer->endTag("Score"); } $writer->endTag("NewHits"); $writer->endTag("Main"); $writer->end(); $output->close(); }
I know data is held in the arrays which i have proved by prints but nothing happens here in the tag part.

Any ideas:
MonkPaul

Replies are listed 'Best First'.
Re: XML tagging problem
by GrandFather (Saint) on Jul 24, 2005 at 19:20 UTC

    That ought to be working except that if you are using dataElement the startTag/endTag pairs are redundant and will generate pairs of nested tags.

    The sample code below works for me and is essentially what you are trying to do:

    use strict; use warnings; use XML::Writer; my $writer = new XML::Writer(); $writer->xmlDecl(); $writer->startTag("Main"); $writer->startTag("NewHits"); $writer->dataElement( "GI" , "Element string"); $writer->endTag("NewHits"); $writer->endTag("Main"); $writer->end();

    Note: reducing your sample code to a small example that demonstrates the problem will not only get a better response from the monks, but may reveal your problem along the way.


    Perl is Huffman encoded by design.
Re: XML tagging problem
by Anonymous Monk on Jul 25, 2005 at 02:15 UTC
    Are you sure that @newHits contains the data you think it contains? Try printing it to standard output before writing the XML file to make sure. Did you mean to pass @newHits as arguments to the writeXML subroutine?