我從一個服務下面的DiffGram XML:我該如何用perl解析diffgam.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 ::這樣簡單解析它:
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是前進的方向。 – Sobrique 2014-11-24 15:46:50
我嘗試了不同組合的ForceArray和KeyAttr,比如'my $ diffgram_hashref = XMLin($ diffgram-> toString(1),KeepRoot => 1,ForceArray => ['xvcs:insert','xvcs:update'],KeyAttr = >爲undef);' 或 '我的$ diffgram_hashref = XMLin($ diffgram->的toString(1),KeepRoot => 1,ForceArray => [ 'xvcs:的DiffGram'],KeyAttr =>爲undef);' 我會看看XML :: Twig,thx的那個提示 – user3337084 2014-11-24 15:54:13
你究竟在努力完成什麼?腳本的預期輸出是什麼?你想按順序處理每個元素嗎? – Sobrique 2014-11-24 16:35:40