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

时间:2018-07-02 23:19:43

标签: 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版本属性。

某些帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

为什么要创建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();

答案 1 :(得分:1)

考虑XSLT,一种专用于转换XML文件的专用语言,例如替换节点和属性文本。 PHP可以使用其php-xsl类运行XSLT 1.0脚本,要求在.ini文件中启用扩展名。

但是,首先需要将相对平坦的JSON转换为PHP可以构建的XML,然后XSLT脚本将递归引用以使用document()函数跨文档映射所需的站点位置值。确保将两个XML文件(原始源文件和json转换文件)都保存在同一目录中。

PHP (JSON转换的XML;仅用于使用循环的地方)

$data = json_decode($site_json, TRUE);

// Create a DOM document 
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;

// Create the root element of XML tree
$root = $dom->createElement("site_locations");
$root = $dom->appendChild($root);

// Iterate json elements for child nodes
foreach ($data as $outer) {      
    foreach ($outer as $k=>$v) {  
        $node = $dom->createElement('site');
        $root->appendChild($node);
        $node->setAttribute("id", $k);
        $node->setAttribute("version", $v);
    }
}

// Output XML content to screen
echo $dom->saveXML();
// <?xml version="1.0"?>
// <site_locations>
//   <site id="ABC01_ABC01" version="9"/>
//   <site id="ABC01_ABC02" version="7"/>
//   <site id="ABC01_ABC03" version="4"/>
//   <site id="ABC01_ABC04" version="1"/>
//   <site id="ABC01_ABC05" version="2"/>
//   <site id="ABC01_ABC06" version="16"/>
//   ...

// Output XML content to file
file_put_contents("SiteLocationsLookups.xml", $dom->saveXML());

XSLT脚本 (另存为.xsl文件,一个特殊的.xml文件)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="identifier">
    <xsl:copy>NLNDW</xsl:copy>
  </xsl:template>

  <xsl:template match="tableLoc">
    <xsl:copy>      
      <xsl:attribute name="id">NDW_01_MT</xsl:attribute>
      <xsl:copy-of select="@version"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="siteLoc">
    <xsl:copy>
      <xsl:variable name="curr_id"><xsl:value-of select="@id"/></xsl:variable>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="version">
         <xsl:value-of select="document('SiteLocationsLookups.xml')//site[@id=$curr_id]/@version"/>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

PHP (处理最终XML)

// Load XML and XSLT files
$doc = new DOMDocument();
$doc->load('Input.xml');

$xsl = new DOMDocument;
$xsl->load('XSLT_Script.xsl');              // REFERENCES JSON CONVERTED XML

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 

// Transform XML source
$newXml = $proc->transformToXML($doc);

// Save output to file
file_put_contents('Output.xml', $newXml);

XML输出

<?xml version="1.0" encoding="utf-8"?>
<country>
  <display>
    <identification>
      <country>nl</country>
      <identifier>NLNDW</identifier>
    </identification>
  </display>
  <region>
    <publicationTime>2018-04-18T09:45:00Z</publicationTime>
    <publicationCreator>
      <identifier>NLNDW</identifier>
    </publicationCreator>
    <tableLoc id="NDW_01_MT" version="14" />
    <measurements>
      <siteLoc id="ABC01_ABC01" version="9" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC02" version="7" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC03" version="4" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC04" version="1" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC17" version="5" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC19" version="2" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC18" version="5" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC15" version="2" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC09" version="7" />
    </measurements>
    <measurements>
      <siteLoc id="ABC01_ABC011" version="" />
    </measurements>
  </region>
</country>