Sta*_*alm 2 xml csv perl parsing
我正在尝试使用ID保存HTTP,就像这样
https://hostname:9060/ers/config/networkdevice/1
Run Code Online (Sandbox Code Playgroud)
在'output.csv'中.之后,我想在我的get请求中使用这些链接,以从管理系统获取有关设备的更多信息.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns3:searchResult total="3" xmlns:ns5="ers.ise.cisco.com" xmlns:ers-v2="ers-v2"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns3="v2.ers.ise.cisco.com">
<ns3:resources>
<ns5:resource id="1"
name="Device1">
<link rel="self" href="https://hostname:9060/ers/config/networkdevice/1"
type="application/xml"/>
</ns5:resource>
<ns5:resource id="2"
name="Device2">
<link rel="self" href="https://hostname:9060/ers/config/networkdevice/2"
type="application/xml"/>
</ns5:resource>
<ns5:resource id="3"
name="Device3">
<link rel="self" href="https://hostname:9060/ers/config/networkdevice/3"
type="application/xml"/>
</ns5:resources>
</ns3:resources>
</ns3:searchResult>
Run Code Online (Sandbox Code Playgroud)
但在使用我的Perl脚本后,我的文件是空的.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
#NOTE: Update this to contain my elements in CSV
my @Fields = qw{id};
my $xml = XMLin('Get_All_Prime.xml', ForceArray => ['link']);
foreach my $link ( @{ $xml->{link} } ) {
print Dumper $link;
foreach my $link ( @{ $xml->{link} } ) {
print join( ',', @{ $link->{href} }{@Fields} ) . "\n";
}
}
open(my $out, '>', 'output.csv') or die "Output: $!\n";
foreach my $link ( @{ $xml->{link} } ) {
print $out join ( ',', @{$link->{href} }{@Fields} ) . "\n";
}
Run Code Online (Sandbox Code Playgroud)
我不确定,我使用的是正确的标签
ForceArray => ['link']
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下我应该使用什么?
XML的标准优秀库是XML :: LibXML和XML :: Twig.
请不要使用XML :: Simple.很久以前它自己的文档就说了
不鼓励在新代码中使用此模块.
很久以前,它自己的作者为XML :: LibXML编写了一个详细的教程.注意这个建议.
一个例子 XML::LibXML
use warnings;
use strict;
use feature 'say';
use XML::LibXML;
my $file = shift @ARGV || 'Get_All_Prime.xml'
my $reader = XML::LibXML->new();
my $doc = $reader->load_xml(location => $file, no_blanks => 1);
my $xpath = '//ns3:searchResult/ns3:resources/ns5:resource/link';
my @links;
foreach my $node ($doc->findnodes($xpath)) {
push @links, $node->findvalue('./@href');
}
say for @links;
Run Code Online (Sandbox Code Playgroud)
<link ...>作为示例,这首先找到元素; 或者你可以直接去'href'
my $xpath = '//ns3:searchResult/ns3:resources/ns5:resource/link/@href';
foreach my $href ($doc->findnodes($xpath) {
push @links, $href->to_literal;
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的库,完全支持使用XML::LibXML.请参阅这篇文章和这篇文章,了解从模块的广泛和结构化文档开始的位置.
我会走出困境,并假设所需的CSV输出文件有行 XML
my $outfile = 'output.csv';
open my $fh_out, '>', $outfile or die "Can't open $outfile: $!";
say $fh_out 'url,id'; # header
my $xpath = '//ns3:searchResult/ns3:resources/ns5:resource/link';
my @links;
foreach my $node ($doc->findnodes($xpath)) {
#say $node->findvalue('./@href'), ' => ', $node->findvalue('../@id');
push @links, [ map { $node->findvalue($_) } qw(./@href ../@id) ];
}
say $fh_out join ',', @$_ for @links;
Run Code Online (Sandbox Code Playgroud)
对于CSV的更广泛的工作,使用Text :: CSV,Text :: CSV_XS的" 瘦包装 " .
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |