Perl - 遍历xml文件并存储特定标记的数据

时间:2013-06-28 20:43:28

标签: xml perl xml-parsing

我需要使用Perl将XML文件中的特定数据(文件名)存储到哈希中。 我一行一行地遍历XML文件,但我仍然坚持如何从schooldata的{​​{1}}属性中获取文件名。

我尝试访问的XML部分是这样的:

file

3 个答案:

答案 0 :(得分:2)

XML::Twig

use warnings;
use strict;
use XML::Twig;

my $twig= new XML::Twig(
    twig_handlers => {
        schooldata => \&schooldata
    }
);

my %files;
$twig->parse(q(
<name number="5">
          <description>
                      <schooldata file="/home/matt/.01"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/matt/.02"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/matt/0.3"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/matt/0.4"></schooldata>
          </description>
</name>
));


use Data::Dumper;
$Data::Dumper::Sortkeys=1;
print Dumper(\%files);

sub schooldata {
    my ($twig, $data) = @_;
    $files{ $data->att('file') }++;
}

__END__

$VAR1 = {
          '/home/matt/.01' => 1,
          '/home/matt/.02' => 1,
          '/home/matt/0.3' => 1,
          '/home/matt/0.4' => 1
        };

答案 1 :(得分:1)

use strict; 
use warnings; 
use 5.014; 

use XML::LibXML;

my $filename = "xml.xml";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);

my %results;

for my $name ($doc->findnodes('/names/name')) {
    my $num = $name->getAttribute('number');

    for my $school ($name->findnodes('description/schooldata')) {
        push @{$results{$num}}, $school->getAttribute('file');
    }
}

use Data::Dumper;
say Dumper(\%results);


--output:--
$VAR1 = {
          '6' => [
                 '/home/betty/.01',
                 '/home/betty/.02',
                 '/home/betty/0.3',
                 '/home/betty/0.4'
               ],
          '5' => [
                 '/home/matt/.01',
                 '/home/matt/.02',
                 '/home/matt/0.3',
                 '/home/matt/0.4'
               ]
        };

xml.xml:

<?xml version="1.0" encoding="UTF-8"?>
<names>
<name number="5">
          <description>
                      <schooldata file="/home/matt/.01"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/matt/.02"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/matt/0.3"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/matt/0.4"></schooldata>
          </description>
</name>

<name number="6">
          <description>
                      <schooldata file="/home/betty/.01"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/betty/.02"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/betty/0.3"></schooldata>
          </description>
          <description>
                      <schooldata file="/home/betty/0.4"></schooldata>
          </description>
</name>
</names>

答案 2 :(得分:-1)

$line = '<schooldata file="/home/matt/.01"></schooldata>';
print $1 if ($line =~ /schooldata file="([^"]+)"/);