从xml文件获取值并将其插入url(GET请求)

时间:2017-03-17 19:45:02

标签: xml perl url xml-parsing

我需要从XML文档中提取一个值并将其添加到URL中。

当我使用Web服务的所有设备发出GET请求时,我得到了这个XML数据

FILE.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns2:searchResult total="3" xmlns:ns2="ers.ise.cisco.com">
    <resource name="TEST_1" id="1" description="example nd">
      <link type="application/xml" href="https://hostname:9060/ers/config/networkdevice/1" rel="self"/>
    </resource>
    <resource name="TEST_2" id="2>
      <link type="application/xml" href="https://hostname:9060/ers/config/networkdevice/2" rel="self"/>
    </resource>
    <resource name="TEST_3" id="3" description="example nd">
      <link type="application/xml" href="https://hostname:9060/ers/config/networkdevice/3" rel="self"/>
    </resource>
  </resources>
</ns2:searchResult>

我的搜索结果仅包含NameID,但我需要设备&#39; IP地址也是如此,为了得到它,我应该在URL

中传递一个GET请求

例如:https://hostname:9060/ers/config/networkdevice/1

我没有找到API来获取所有设备的更多信息。

有谁知道如何循环我的ID?我可以将值插入我的网址吗?

我当前的代码

#!/usr/bin/env perl

use strict;
use warnings;

use XML::Twig;
use LWP 5.64;
use LWP::UserAgent;
use MIME::Base64;
use REST::Client;
use IO::Socket::SSL;
use HTTP::Headers;
use HTTP::Request;

my $xml1 = XML::Twig->new->parsefile('FILE.xml');

foreach my $id ($xml1->root->att('id'))
# .........
# I don't know what I should make here


#Create a user agent object
my $ua = LWP::UserAgent->new(ssl_opts=> {
    SSL_verify_mode => SSL_VERIFY_NONE(),
    verify_hostname => 0,
});

#Create a request
my $uri = 'https://hostname:9060/ers/config/networkdevice'; # I need to insert my ID into this URL
my $header = HTTP::Headers->new;
$header->Header(Accept => 'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml');
my $req = HTTP::Request->new('GET', $uri, $header);


$req->authorization_basic("user", "password");

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

# Check the outcome of the response
if ( $res->is_success ) {
    print $res->content;
} 
else {
    print $res->status_line, "n";
}

之后,我需要将所有设备保存到一个XML文件中。

1 个答案:

答案 0 :(得分:0)

您需要的网址位于每个资源中href元素的link属性中。只需获取该属性的值并直接使用