in reply to XML Parsing Out of Memory error

If you dump $xml_ref you will find that the structure is not exactly what you think it is, xml_ref->{'QuestionList'} is an array ref, and what you want to display is $xml_ref->{'QuestionList'}->[0]->{'Question'}->{'Answer'};.

And Perl could be more explicit about the error!

Replies are listed 'Best First'.
Re: Re: XML Parsing Out of Memory error
by JackHammer (Acolyte) on Feb 28, 2002 at 17:18 UTC
    Sorry about the confusion, this is a snippet from a larger xml structure, I should have included more XML for the example. Try the following code instead:
    #!/usr/bin/perl use XML::Simple; use Data::Dumper; my $xml = qq|<?xml version="1.0" standalone="no"?> <!DOCTYPE document SYSTEM "whocares.dtd"> <document> <QuestionList> <Question> <Id>3</Id> <Text>Are dingos your friend?</Text> <Answer>Satisfactory</Answer> </Question> <Question> <Id>3</Id> <Text>Should I have hit preview a second time?</Text> <Answer>Yes</Answer> </Question> </QuestionList> <QuestionList> <Question> <Id>11</Id> <Text>Should this run out of memory?</Text> <Answer>No</Answer> </Question> </QuestionList> </document>|; my $xml_ref = XMLin($xml); print Dumper $xml_ref; print $xml_ref->{'QuestionList'}->{'Question'}->[0]->{'Answer'};
    in the full code I check to see if this is a list of questions or not... But if you try that one out Question indeed is an array ref as my print would suggest.

      Then you are looking for:

      $xml_ref->{'QuestionList'}->[0]->{Question}->[0]->{Answer}

      I have found using the debugger pretty useful in such a case: once the XML had been XMLin'd, I x'd $xml_ref->{'QuestionList'}, saw it was an array, then x'd $xml_ref->{'QuestionList'}->[0], then $xml_ref->{'QuestionList'}->[0]->{Question} and figured out it was also an array... hence the final code. I often use this technique to debug complex data structures.