0
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&amp;G/Pantene/Asia/T03 Pallet Pattern/PAL_23x3x69_P&amp;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
enter image description here

2
  • Here you have an example how to use XML::LibXML Commented Jun 27, 2014 at 6:59
  • //body is short for /descendant:body, which means all body elements under root. You want child:body, or body for short. Commented Jun 27, 2014 at 15:41

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please check my edited question. I used the same method but its not looping correctly. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.