Perl读取XML值

时间:2014-06-27 06:55:21

标签: xml perl xml-libxml

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>

以上代码返回&#34; Role = Manager&#34;。如何获得其他价值?例如Nameagesexdepartment

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>        

输出
enter image description here

1 个答案:

答案 0 :(得分:1)

如果您的示例数据实际上不仅包含根节点,那么它会有所帮助。以下内容应演示如何提取子节点和属性。

有关xpath的示例,请查看: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>

输出:

Name:  Smith
 Role: manager
Name:  John
 Role: janitor
Name:  Sally
 Role: director

对于修订后的XML,以下内容可能更接近您的需求:

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
}