【发布时间】:2018-12-11 04:32:05
【问题描述】:
我正在尝试更改 xml 文件。示例文件如下所示
<?xml version="1.0" encoding="UTF-8" ?>
<country>
<display>
<identification>
<country>nl</country>
<identifier>ABC01</identifier>
</identification>
</display>
<region>
<publicationTime>2018-04-18T09:45:00Z</publicationTime>
<publicationCreator>
<identifier>ABC01</identifier>
</publicationCreator>
<tableLoc id="ABC01_SITE_TABLE" version="14"/>
<measurements>
<siteLoc id="ABC01_ABC01" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC02" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC03" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC04" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC17" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC19" version="9"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC18" version="9"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC15" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC09" version="7"/>
</measurements>
<measurements>
<siteLoc id="ABC01_ABC011" version="7"/>
</measurements>
</region>
</country>
首先我需要更改 2 个标识符的标签值
<identifier>ABC01</identifier>
到
<identifier>NLNDW</identifier>
成功了。然后我不得不改变
的属性<tableLoc id="ABC01_SITE_TABLE" version="14"/>
到
<tableLoc id="NDW_01_MT" version="14"/>
最后我需要用 json 文件中的相应 id 覆盖所有 siteloc 属性版本:
{
"ABC01": {
"ABC01_ABC01": 9,
"ABC01_ABC02": 7,
"ABC01_ABC03": 4,
"ABC01_ABC04": 1,
"ABC01_ABC05": 2,
"ABC01_ABC06": 16,
"ABC01_ABC07": 3,
"ABC01_ABC08": 8,
"ABC01_ABC09": 7,
"ABC01_ABC10": 6,
"ABC01_ABC11": 4,
"ABC01_ABC12": 3,
"ABC01_ABC13": 2,
"ABC01_ABC14": 2,
"ABC01_ABC15": 2,
"ABC01_ABC16": 3,
"ABC01_ABC17": 5,
"ABC01_ABC18": 5,
"ABC01_ABC19": 2,
"ABC01_ABC20": 3,
"ABC01_ABC21": 5,
"ABC01_ABC22": 39,
"ABC01_ABC23": 23,
"ABC01_ABC24": 13,
"ABC01_ABC25": 1,
"ABC01_ABC26": 1,
"ABC01_ABC27": 3
},
"ABC02": {
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 2,
"ABC02_ABC02": 3,
"ABC02_ABC02": 2
},
"VWY09": {
"VWY09_VWY10": 1,
"VWY09_VWY11": 1,
"VWY09_VWY12": 1,
"VWY09_VWY13": 1,
"VWY09_VWY14": 1,
"VWY09_VWY15": 1,
"VWY09_VWY16": 1,
"VWY09_VWY17": 1,
"VWY09_VWY18": 1,
"VWY09_VWY19": 1
},
"GAD02": {
"GAD02_XRZ01": 2,
"GAD02_XRZ02": 2,
"GAD02_XRZ03": 2,
"GAD02_XRZ04": 2,
"GAD02_XRZ05": 2,
"GAD02_XRZ06": 2,
"GAD02_XRZ07": 2,
"GAD02_XRZ08": 2
}
}
例如
<measurements>
<siteLoc id="ABC01_ABC03" version="7"/>
</measurements>
需要成为:
<measurements>
<siteLoc id="ABC01_ABC03" version="4"/>
</measurements>
到目前为止我的代码:
$identification = new DomDocument;
$identification2 = new DomDocument;
$identification3 = new DomDocument;
$identification_node = $identification ->createElement('identification');
$identification_node2 = $identification2 ->createElement('publicationCreator');
$identification_node3 = $identification3 ->createElement('tableLoc');
// Add some children
$identification_node->appendChild($identification->createElement('country', 'nl'));
$identification_node->appendChild($identification->createElement('identifier', 'NLNDW'));
$identification_node2->appendChild($identification2->createElement('identifier ', 'NLNDW'));
$newnode3 = $identification3->appendChild($identification_node3);
$newnode3->setAttribute("align", "left");
$identification->appendChild($identification_node);
$identification2->appendChild($identification_node2);
$dom = new DomDocument;
$dom->loadXML($xml);
$xpath = new DOMXpath($dom);
$xpath1 = new DOMXpath($dom);
$xpath2 = new DOMXpath($dom);
$nodelist = $xpath->query('/country/display/identification');
$nodelist1 = $xpath1->query('/country/region/publicationCreator');
$oldnode = $nodelist->item(0);
$oldnode1 = $nodelist1->item(0);
$newnode = $dom->importNode($identification->documentElement, true);
$newnode1 = $dom->importNode($identification2->documentElement, true);
$oldnode->parentNode->replaceChild($newnode, $oldnode);
$oldnode1->parentNode->replaceChild($newnode1, $oldnode1);
echo $dom->saveXML($dom);
$dom->save('result.xml');
我知道如何遍历 xml,但我不知道如何在 json 文件中找到匹配的 siteloc id 属性并用 json 中的版本覆盖 siteloc 版本属性。
我们将不胜感激
【问题讨论】:
标签: php json xml domdocument