我从服务获取以下diffgram XML:
<?xml version="1.0"?>
<xvcs:diffgram xmlns:xvcs="http://www.xvcs.org/">
<xvcs:update id="7" first-child-of="/opt/node/node[1]">
<xvcs:attr-update name="location" old-value="???" new-value="testlocation"/>
</xvcs:update>
<xvcs:update id="35" follows="/opt/node/node[2]">
<xvcs:attr-update name="URL" old-value="/" new-value="/testurl/"/>
</xvcs:update>
<xvcs:insert id="75" first-child-of="/opt">
<node node_id="/1234" location="new location" URL="/newurl"></node>
</xvcs:insert>
</xvcs:diffgram>
我以这种方式用XML :: Simple解析它:
my $diffgram_hashref = XMLin($diffgram->toString(1),
KeepRoot => 1,
ForceArray => 1,
);
$logger->debug( dump($diffgram_hashref) );
并得到以下结果:
{
"xvcs:diffgram" => [
{
"xmlns:xvcs" => "http://www.xvcs.org/",
"xvcs:insert" => {
75 => {
"first-child-of" => "/opt",
"node" => [
{
node_id => "/1234",
location => "new location",
URL => "/newurl",
},
],
},
},
"xvcs:update" => {
7 => {
"first-child-of" => "/opt/node/node[1]",
"xvcs:attr-update" => {
location => { "new-value" => "testlocation", "old-value" => "???" },
},
},
35 => {
"follows" => "/opt/node/node[2]",
"xvcs:attr-update" => {
URL => { "new-value" => "/testurl/", "old-value" => "/" },
},
},
},
},
],
}
我尝试了几种ForeArray / KeyAttr组合,但是为了将diffgram语句(更新,插入)作为数组以正确的顺序进行处理,我没有实现:
{
"xvcs:diffgram" => [
{
"xvcs:update" => {
7 => {
"first-child-of" => "/opt/node/node[1]",
"xvcs:attr-update" => {
location => { "new-value" => "testlocation", "old-value" => "???" },
},
}
}
},
{
"xvcs:update" => {
35 => {
"follows" => "/opt/node/node[2]",
"xvcs:attr-update" => {
URL => { "new-value" => "/testurl/", "old-value" => "/" },
},
},
}
},
{
"xvcs:insert" => {
75 => {
"first-child-of" => "/opt",
"node" => [
{
node_id => "/1234",
location => "new location",
URL => "/newurl",
},
],
},
},
}
]
}
有人可以帮我吗?
该程序使用XML::Twig
模块执行您要执行的操作。我已经忽略了顶级哈希键,xvcs:diffgram
因为该哈希只有一个元素。同样适用于数组中的每个哈希-我更希望将element标签视为辅助哈希的元素之一的值,因为从目前的角度来看,您具有数组上的哈希。但是,我已经按照您的描述保留了此结构。
我还将id
,name
和URL
属性保留为简单的哈希元素,而不是像您的示例中那样对待它们。
我Data::Dump
仅用于显示根据数据构建的结构。
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new;
$twig->parse(\*DATA);
my @data;
for my $node ( $twig->root->children ) {
my $atts = $node->atts;
for my $child ($node->children) {
$atts->{$child->tag} = $child->atts;
}
push @data, { $node->tag => $atts };
}
use Data::Dump;
dd \@data;
__DATA__
<?xml version="1.0"?>
<xvcs:diffgram xmlns:xvcs="http://www.xvcs.org/">
<xvcs:update id="7" first-child-of="/opt/node/node[1]">
<xvcs:attr-update name="location" old-value="???" new-value="testlocation"/>
</xvcs:update>
<xvcs:update id="35" follows="/opt/node/node[2]">
<xvcs:attr-update name="URL" old-value="/" new-value="/testurl/"/>
</xvcs:update>
<xvcs:insert id="75" first-child-of="/opt">
<node node_id="/1234" location="new location" URL="/newurl"></node>
</xvcs:insert>
</xvcs:diffgram>
输出
[
{
"xvcs:update" => {
"first-child-of" => "/opt/node/node[1]",
"id" => 7,
"xvcs:attr-update" => {
"name" => "location",
"new-value" => "testlocation",
"old-value" => "???",
},
},
},
{
"xvcs:update" => {
"follows" => "/opt/node/node[2]",
"id" => 35,
"xvcs:attr-update" => {
"name" => "URL",
"new-value" => "/testurl/",
"old-value" => "/",
},
},
},
{
"xvcs:insert" => {
"first-child-of" => "/opt",
"id" => 75,
"node" => {
location => "new location",
node_id => "/1234",
URL => "/newurl",
},
},
},
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句