Reputation: 6403
use strict;
use warnings;
use XML::LibXML;
my $xml = XML::LibXML->load_xml(IO => \*DATA);
for my $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 to get other values? For an example Name
.age
,sex
and department
.
EDITED
use strict;
use warnings;
use XML::LibXML;
my $xml = XML::LibXML->load_xml(IO => \*DATA);
for my $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
Upvotes: 0
Views: 88
Reputation: 35198
It helps if your sample data actually includes more than just a root node. The following should demonstrate how to pull child nodes and attributes.
For examples of xpaths, just check out: XPath Examples
use strict;
use warnings;
use XML::LibXML;
my $xml = XML::LibXML->load_xml(IO => \*DATA);
for my $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 your revised XML, the following might get closer to what you want:
for my $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
}
Upvotes: 1