Perl Read XML value -
use strict; use warnings; use xml::libxml; $xml = xml::libxml->load_xml(io => \*data); $role ($xml->findnodes('//@role')) { print $role->value; } __data__ <?xml version='1.0'?> <employee> <name>smith</name> <age>43</age> <sex>m</sex> <department role='manager'>operations</department> </employee>
above code returns "role = manager". how other values? example name
.age
,sex
, department
.
edited
use strict; use warnings; use xml::libxml; $xml = xml::libxml->load_xml(io => \*data); $employee ($xml->findnodes('//copy')) { print "name: ", $employee->findvalue('//body'), "\n"; print " role: ", $employee->findvalue('copyelement/@copyelementtype'), "\n"; } __data__ <?xml version="1.0" encoding="utf-8" standalone="no" ?> <copycontent agentname="" agentversion="" nonamespaceschemalocation="" xmlns1=""> <revisionhistory revisionnumber=""> <revision author="tester" revisionnumber="01" timestamp="2014052103:51:04" type=""/> </revisionhistory> <project projectid="112233" projectname="" region=""> <poas> <poa id="" locales="" name=""/> </poas> </project> <copy> <copyelement copyelementtype="template_name" copysourceref="" id="" linkedstate="notlinkedtoartwork" locale="" poas="" panels="" sourceref=""> <body> <countrycode/> <p>auto-template1</p> </body> </copyelement> <graphicelement descriptivename="legendimages" id="0001" type="link" ref=" file:////volumes/schawk asia/asia prodart/297620a/090 deliverables/legend/97005846_d_legend_legend.pdf"/> <graphicelement descriptivename="pallet_pattern" id="0001" type="link" ref=" file:////volumes/tornado shipper library/p&g/pantene/asia/t03 pallet pattern/pal_23x3x69_p&g 1329_100_v0001.pdf"/> <copyelement copyelementtype="workername" copysourceref=" a6" id="" linkedstate="notlinkedtoartwork" locale="english" poas="" panels="" sourceref=" brand name"> <body> <countrycode/> <p>alan smith</p> </body> </copyelement> <copyelement copyelementtype="empname" copysourceref=" a6" id="" linkedstate="notlinkedtoartwork" locale="english" poas="" panels="" sourceref=" brand name"> <body> <countrycode/> <p>brendan froesr</p> </body> </copyelement> </copy> <private/> </copycontent>
output
it helps if sample data includes more root node. following should demonstrate how pull child nodes , attributes.
for examples of xpaths, check out: xpath examples
use strict; use warnings; use xml::libxml; $xml = xml::libxml->load_xml(io => \*data); $employee ($xml->findnodes('//employee')) { print "name: ", $employee->findvalue('name'), "\n"; print " role: ", $employee->findvalue('department/@role'), "\n"; } __data__ <?xml version='1.0'?> <root> <employee> <name>smith</name> <age>43</age> <sex>m</sex> <department role='manager'>operations</department> </employee> <employee> <name>john</name> <age>34</age> <sex>m</sex> <department role='janitor'>maintenance</department> </employee> <employee> <name>sally</name> <age>18</age> <sex>f</sex> <department role='director'>human resources</department> </employee> </root>
outputs:
name: smith role: manager name: john role: janitor name: sally role: director
for revised xml, following might closer want:
for $copy ($xml->findnodes('//copy/copyelement')) { (my $body = $copy->findvalue('body')) =~ s/^\s+|\s+$//g; (my $type = $copy->findvalue('@copyelementtype')) =~ s/^\s+|\s+$//g; print <<"end_copy"; name: $body role: $type end_copy }
Comments
Post a Comment