使用XML :: LibXML解析xml;和perl

时间:2013-02-16 01:07:15

标签: perl parsing xml-parsing

我很难弄清楚下面布局的xml路径是什么。

<?xml version="1.0"?>
<Root Name="\\Admin1\ROOT" State="1" Timeout="300" >
       <Target Server="Admin`" Folder="ROOT" State="2"/> 

       <Link Name="users\foo" State="1" Timeout="300" >
              <Target Server="server1" Folder="folder1\foo" State="2"/>
       </Link>


      <Link Name="users\bar" State="1" Timeout="300" >
             <Target Server="server2" Folder="folder2\bar" State="2"/>
      </Link>
</Root>

我似乎无法弄清楚如何使波纹管代码打印每个链接名称。

use strict;use warnings;

my $filename = 'dfs_dump.xml';

use XML::LibXML;
my $parser = XML::LibXML->new();

my $doc    = $parser->parse_file($filename);
foreach my $link ($doc->findnodes('/Root Name/Target Server')) {
    my($title) = $link->findnodes('/Link Name');
    print $title->to_literal, "\n" 
}

1 个答案:

答案 0 :(得分:1)

for my $link_node ($doc->findnodes('/Root/Link')) {
    print $link_node->getAttribute('Name') // "[none]", "\n";
}

for my $link_name ($doc->findnodes('/Root/Link/@Name')) {
    print $link_name->getData(), "\n";
}