in reply to searching the XML file for certain nodes

use strict; use XML::Rules; my $filter = XML::Rules->new( style => 'filter', rules => { _default => 'raw', p => sub { my ($tag, $attr) = @_; if ($attr->{v} eq 'YES') { return $tag => $attr } else { return; } }, }, ); $filter->filter(\*DATA); __DATA__ <?xml version="1.0" encoding="UTF-8" ?> <e> <p id="1" v="YES"> <e1>The</e1> <e2>Bye</e2> </p> <p id="2" v="NO"> <e1>Border</e1> <e2>Lamp</e2> </p> </e>

Unlike the XML::LibXML and the presented XML::Twig solutions, this one doesn't keep the whole (original or resulting) document in memory, instead it only stores the contents of one <p> at any time. If the file is huge and contains a lot of small <p> tags, this may make a big difference.

Replies are listed 'Best First'.
Re^2: searching the XML file for certain nodes
by mirod (Canon) on Mar 10, 2009 at 15:45 UTC

    Hey, I can do low-memory (and cryptic!) too. And I'll raise you with a one-liner:

    perl -MXML::Twig -e'XML::Twig->parse( twig_roots => { q{p[@v="NO"]} => 1 }, twig_print_outside_roots => 1, shift)' myfile.xml

    Let's play XML golf! (in which case I'll have to release a special golf-edition of XML::Twig, with shorter option names ;--)

      I never said XML::Twig can't do it without keeping all the stuff in memory. All I said was that the solutions presented so far do keep the whole filtered data in memory :-)

      I can't complete with that script in the number of characters. Unless I make changes in the module. Whether it's cryptic depends on the beholder. It ain't for me, but who am I to tell, I'm the module author :-).

        module author, mothor, scary mother :)