in reply to Re^2: Is there any XML reader like this?
in thread Is there any XML reader like this?
I'm have no idea why you call XML::LibXML a monster compared to XML::Simple.
use XML::Simple qw( :strict XMLin ); local $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; my $stations = XMLin( \*DATA, ForceArray => 1, KeyAttr => [] ); for my $station_name (keys %$stations) { say $station_name; my $station = $stations->{$station_name}[0]; for my $ip (@{ $station->{ips} // [] }) { say " $ip"; } }
use XML::LibXML qw( ); my $root = XML::LibXML->load_xml( IO => \*DATA )->documentElement; for my $station ($root->findnodes('*')) { say $station->getName; for my $ip ($station->findnodes('ip')) { say " ".$ip->textContent; } }
And that's not even mentioning the fact that XML::LibXML is 20x faster* and able to handle so much more stuff than XML::Simple (including every day stuff).
* — That assumes XML::Parser is used as XML::Simple's backend. XML::LibXML is 10,000x faster than XML::Simple's common default of XML::SAX::PurePerl (which handles encodings really badly).
Update: Fixed an error in XML::Simple code.
Update: Fixed an error in XML::LibXML code. ("IO" was mispelled, and the XPath was wrong.)
|
|---|