【发布时间】:2018-06-24 08:51:09
【问题描述】:
我正在尝试使用 ID 保存 HTTP,例如这个
https://hostname:9060/ers/config/networkdevice/1
在“输出.csv”中。之后,我想在我的获取请求中使用这些链接从管理系统获取有关设备的更多信息。
<?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>
但是使用我的 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";
}
我不确定,我使用了正确的标签
ForceArray => ['link']
但是在这种情况下我应该使用什么?
【问题讨论】:
-
XML::Parser,XML::Simple 的默认解析器,不支持命名空间。避免使用 all-around bad 解析器 XML::Simple。我推荐 XML::LibXML。
-
你想要的输出是什么?