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

Hi Monks,

I'm trying to wrap my knoggen around XML::Parser and am having no luck so far...

I've been through the perl docs for it but cant understand how to easily use the output from XML::Parser->parse().

I thought I would be able to do something like:
use XML::Parser; use XML::Parser::EasyTree; $XML::Parser::EasyTree::Noempty=1; my $p = new XML::Parser(Style => 'EasyTree'); my $tree = $p->parse($xml); my $node1 = ${$tree[0]}{name};
but node1 is returning a blank.

The XML i am using for testing is:
<?xml version="1.0" encoding="UTF-8"?> <search_results count="0"> <query>"flaherty jm" [ALL-FIELDS]</query> </search_results>

Could anyone suggest some better reading for me to help me understand how this works?

Cheers,
Reagen

Replies are listed 'Best First'.
Re: understanding XML::Parser
by Thelonius (Priest) on Jul 17, 2006 at 05:57 UTC
    When I use strict; I get the error Global symbol "@tree" requires explicit package name. This is a good clue.

    When I change

    my $node1 = ${$tree[0]}{name};
    to
    my $node1 = ${$tree->[0]}{name};
    everything works.

    $tree[0] is the zero'th element of @tree.

    $tree->[0] is the zero'th element of tree that is referenced by $tree.

      Thanks.

      Is there an easier way to get a value if I know only the name of the element?

      For example: if i want the value of count which resides in search_results?

        Based on my XML decision tree, you may want to use a technology that abstracts the XML tree such that it gives you XPath capabilities. I just load the entire XML file into memory, then ask the object to give me the elements that match my criteria.