【发布时间】:2015-01-22 09:12:01
【问题描述】:
我从服务中获得以下 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 而不是 XML::Simple 是前进的方向。
-
我尝试了 ForceArray 和 KeyAttr 的不同组合,例如
my $diffgram_hashref = XMLin($diffgram->toString(1), KeepRoot => 1, ForceArray => ['xvcs:insert', 'xvcs:update'], KeyAttr => undef);或my $diffgram_hashref = XMLin($diffgram->toString(1), KeepRoot => 1, ForceArray => ['xvcs:diffgram'], KeyAttr => undef);我会看看 XML::Twig,谢谢这个提示 -
你实际上想要完成什么?您的脚本的预期输出是什么?您想按顺序处理每个元素吗?
-
预期输出应该是按正确顺序排列的语句数组(更新、插入)。不幸的是 id 不是连续的,所以这不是要排序的标识符。
-
如果您显示了您希望从该数据中获得的 “哈希数组”,我很乐意编写一个使用
XML::Twig为您创建它的脚本。