Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Is it possible to parse an XML file recursively using XML::Twig?

by mr_ron (Chaplain)
on Oct 20, 2015 at 15:37 UTC ( [id://1145449]=note: print w/replies, xml ) Need Help??


in reply to Is it possible to parse an XML file recursively using XML::Twig?

I like XPath and found some XPath solutions to the problem on stackoverflow here including this one: //*[not(child::*)]. XML::Twig didn't seem to handle the XPath expressions on stackoverflow but XML::XPath did. XML::XPath also comes with an 'xpath' grep like utility that might give you a solution as simple as:

xpath -e '//*[not(child::*)]' books.xml

If you want to do Perl coding for further processing the solution is still pretty simple

use strict; use warnings; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new(filename => 'books.xml'); my $nodeset = $xp->find('//*[not(child::*)]'); foreach my $node ($nodeset->get_nodelist) { print "FOUND:", XML::XPath::XMLParser::as_string($node), "\n"; }
Ron

Replies are listed 'Best First'.
Re^2: Is it possible to parse an XML file recursively using XML::Twig?
by Jenda (Abbot) on Oct 21, 2015 at 16:27 UTC

    Please corect me if I'm wrong but ... this will parse the whole (huge) file and produce an even huger maze of objects in memory before you even get a chance to call your find(). A huge waste of memory for such a task.

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

Re^2: Is it possible to parse an XML file recursively using XML::Twig?
by Ppeoc (Beadle) on Oct 30, 2015 at 17:57 UTC
    Ron, I really like your solution! I was using XML::parse so far. But this one definitely seems to work. Thank you

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1145449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found