Issue with XML::Bare perl -
Issue with XML::Bare perl -
i'm using xml::bare module in perl.
my xml like:
<xml> <element num="1"> <num>10</num> </element> <element num="2"> <num>20</num> </element> </xml> i want extract out value contained in kid <num> tag. i.e. want 10 , 20 output. when value using $xml->{element}->[$i]->{num}->{value} returns 1 adn 2 i.e returns value of num attribute instead of kid node. can please help me how handle cases when attribute , kid name same.
thanks
if xml::bare or xml::simple used, should limited basic xml reading. given info has attribute , kid node share name fails limitation.
in truth though, unless there specific reason not, 1 should stick more modern xml parsers such xml::twig , xml::libxml , total powerfulness of xpath support.
the next parses xml using both of these 2 modules:
use strict; utilize warnings; utilize xml::libxml; utilize xml::twig; $data = {local $/; <data>}; print "xml::libxml\n"; $xml = xml::libxml->load_xml(string => $data); $node ($xml->findnodes('//element/num')) { print $node->textcontent(), "\n"; } print "xml::twig\n"; $t = xml::twig->new( twig_handlers => { '//element/num' => sub { print $_->text(), "\n" }, }, ); $t->parse( $data ); __data__ <xml> <element num="1"> <num>10</num> </element> <element num="2"> <num>20</num> </element> </xml> outputs:
xml::libxml 10 20 xml::twig 10 20 xml perl parsing
Comments
Post a Comment