使用perl从xml中提取属性

时间:2014-07-06 11:19:55

标签: perl

xml文件就像这样

<properties>
    <property object="tournament_stage" objectFK="820745" type="metadata" name="International" value="no" del="no" n="0" ut="2010-07-28 08:58:34" id="10533355"/>
    <property object="tournament_stage" objectFK="820745" type="metadata" name="Cup" value="no" del="no" n="0" ut="2010-07-28 08:58:34" id="10533356"/>
    <property object="tournament_stage" objectFK="820745" type="metadata" name="Live" value="yes" del="no" n="0" ut="2010-07-28 08:58:35" id="10533357"/>
    <property object="tournament_stage" objectFK="820745" type="metadata" name="Note" value="LIVE:DK: Goalscores (from the list) and Spectators (Redcards, Yellowcards, (Lineup, Formation and Subs. added from Pakistan For all light matches (not livestats matches)) (Squads : DK)" del="no" n="1" ut="2010-08-20 18:34:32" id="10533358"/>
    <property object="tournament_stage" objectFK="820745" type="metadata" name="Ranking" value="1" del="no" n="0" ut="2010-07-28 08:58:35" id="10533359"/>
</properties>

这里是我要提取objectFK值和id的xml文件,我也想计算对象数 这意味着如果树中的起始行是“属性对象”,则将其计为1。 这里有3个对象。

我只想使用cpan模块。

1 个答案:

答案 0 :(得分:2)

只需使用像XML::LibXMLXML::Twig这样的XML解析器:

use strict;
use warnings;

use XML::LibXML;

my $xml = XML::LibXML->load_xml(IO => \*DATA);

for my $property ($xml->findnodes('//property')) {
    print $property->getAttribute('objectFK') . ' ' . $property->getAttribute('id') . "\n";
}

__DATA__
    <properties>
        <property object="tournament_stage" objectFK="820745" type="metadata" name="International" value="no" del="no" n="0" ut="2010-07-28 08:58:34" id="10533355"/>
        <property object="tournament_stage" objectFK="820745" type="metadata" name="Cup" value="no" del="no" n="0" ut="2010-07-28 08:58:34" id="10533356"/>
        <property object="tournament_stage" objectFK="820745" type="metadata" name="Live" value="yes" del="no" n="0" ut="2010-07-28 08:58:35" id="10533357"/>
        <property object="tournament_stage" objectFK="820745" type="metadata" name="Note" value="LIVE:DK: Goalscores (from the list) and Spectators (Redcards, Yellowcards, (Lineup, Formation and Subs. added from Pakistan For all light matches (not livestats matches)) (Squads : DK)" del="no" n="1" ut="2010-08-20 18:34:32" id="10533358"/>
        <property object="tournament_stage" objectFK="820745" type="metadata" name="Ranking" value="1" del="no" n="0" ut="2010-07-28 08:58:35" id="10533359"/>
    </properties>

输出:

820745 10533355
820745 10533356
820745 10533357
820745 10533358
820745 10533359