php - 在php中用json文件替换xml值和属性

标签 php json xml domdocument

我正在尝试更改 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 版本属性。

如果有帮助,我们将不胜感激

最佳答案

为什么要创建 4 个不同的 DOM 文档?要替换您只需要的数据,请加载原始数据并替换它。

$document = new DOMDocument();
// load the original data
$document->loadXml($xml);
$xpath = new DOMXpath($document);

// iterate any "identifier" element node
foreach ($xpath->evaluate('//identifier') as $identifier) {
  // change its content
  $identifier->textContent = 'NLNDW';
}
// iterate any "id" attribute of an "tableLoc" node 
foreach ($xpath->evaluate('//tableLoc/@id') as $tableLocId) {
  // change its value
  $tableLocId->value = 'NDW_01_MT';
}

// decode json, force arrays
$map = json_decode($json, TRUE);

// iterate any "siteLoc" element node in the document
foreach ($xpath->evaluate('//siteLoc[@id]') as $siteLocation) {
  // read the "id"attribute
  $id = $siteLocation->getAttribute('id');
  // the json seems to be grouped by the first part
  $groupId = strstr($id, '_', TRUE);
  // check if here is a new value
  if (isset($map[$groupId][$id])) {
    // change the version attribute
    $siteLocation->setAttribute('version', $map[$groupId][$id]);
  } 
}

// output/save the modified document
echo $document->saveXml();

关于php - 在php中用json文件替换xml值和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51144768/

相关文章:

php - 通过单个查询获得类别和子类别

php - 使用 gcc 和 php 编译 C 代码以实现多输入

javascript - Oracle 9i - 将 Varchar2 转换为 javascript 转义的 UTF-8?

javascript - 循环创建多个点击事件

xml - ASP.NET JSON Web 服务总是返回用 XML 包装的 JSON 响应

php - 仍然显示相同的错误: cURL error 60: SSL certificate problem: unable to get local issuer certificate

php - MySQL - 更新和删除查询不起作用

java - 如何使用 Jackson/其他库将 json 响应对象映射到首选格式?

php - 每周将 XML 文件保存到数据库

java - 从 xml 中查找所有有效的 xpath