http://www.perlmonks.org?node_id=1009941

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

Hello everyone! Could anyone tell if there is a Perl XML parser that reads input XML file string by string, not the whole at once?.. The reason of such question is - I have a real big XML, about 1G, and want Perl to find all "id" attributes of nodes with a particular path, say, "root/mynode":
<root> blah-blah, some nodes here <mynode id=500> and some nodes here also </mynode> ... blah-blah, some nodes here <mynode id=500> and some nodes here also </mynode> </root>
Thanks in advance for your help P.S. Does XML::XPath do so?..

Replies are listed 'Best First'.
Re: A Perl module to parse an XML string by string?
by toolic (Bishop) on Dec 21, 2012 at 17:40 UTC
    XML::Twig "A perl module for processing huge XML documents in tree mode."
      Thank you! That is just what I need.
Re: A Perl module to parse an XML string by string?
by runrig (Abbot) on Dec 21, 2012 at 18:24 UTC
    An example with XML::Rules
    use XML::Rules; my $xml = <<XML; <root> <mynode id='500'> </mynode> <mynode id='500'> </mynode> </root> XML my @rules = ( # Only get 'mynode' directly under root mynode => [ '/root' => sub { print "$_[1]->{id}\n"; return }, _defau +lt => undef ], _default => undef, ); my $xr = XML::Rules->new(rules => \@rules); $xr->parse($xml);
Re: A Perl module to parse an XML string by string?
by choroba (Cardinal) on Dec 22, 2012 at 13:25 UTC
    You can use XML::LibXML::Reader.

    Note that your example XML is not well formed: attribute values must be enclosed in single or double quotes.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ