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

Hello Wise Monks

I have a large XML file and I need to pull the data out a certain tags in the file. I figure I would use this in my script ...

use XML::Parser; my $p = XML::Parser->new(Style => 'Tree'); my $tree = $p->parsefile('foo.xml');
I can see I get the expected data by doing a ...
print Dumper( $tree );
... but I cannot figure out how I am support to transfer the tree until I extract the data I am want.

thanks

Replies are listed 'Best First'.
Re: How do I traverse the data struct returned from parsing an XML::Parse 'Tree'?
by CountZero (Bishop) on Mar 08, 2011 at 20:21 UTC
    The canonical way to extract parts of XML, tags or data is to use XPath.

    Perl as an implementation of XPath in XML::XPath, XML::LibXML, XML::Twig and probably a few more.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      thanks! XML::XPath does what I need to do.
Re: How do I traverse the data struct returned from parsing an XML::Parser 'Tree'?
by toolic (Bishop) on Mar 08, 2011 at 20:07 UTC
    It sounds like you do not yet have much time invested in XML::Parser. If that is the case, I recommend switching to a parser with a method-based interface. I switched to XML::Twig because I found it easier to learn than some of the other parsers. It has a good tutorial , a handy quick reference and its author provides great support. It was much easier for me to use than XML::Parser.
Re: How do I traverse the data struct returned from parsing an XML::Parse 'Tree'?
by Jenda (Abbot) on Mar 09, 2011 at 11:04 UTC

    XML::Parser is very seldom the right tool to use directly. If you do want to end up with a transformed data structure, then maybe XML::Rules is a good candidate. It'll let you specify the transformation rules and will extract just the data you need and transform them as it goes through the file. So it can easily handle even very big files unlike the modules that first build a maze of objects out of the whole file and then query the maze using xpath or something.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.