-1

我需要使用 Perl 将 XML 文件中的特定数据(文件名)存储到哈希中。我正在逐行遍历 XML 文件,但我被困在如何从schooldata'sfile属性中获取文件名。

我试图访问的 XML 部分如下所示:

<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="10">
...
</name>
4

3 回答 3

2

XML::树枝

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
        };
于 2013-06-28T21:02:04.363 回答
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>
于 2013-06-28T21:02:19.923 回答
-1
$line = '<schooldata file="/home/matt/.01"></schooldata>';
print $1 if ($line =~ /schooldata file="([^"]+)"/);
于 2013-06-28T21:07:52.280 回答