【问题标题】:How to replace whole node value in XML如何替换 XML 中的整个节点值
【发布时间】:2013-02-22 15:15:27
【问题描述】:

我有 2 个 XML 字符串:

$xml = '<?xml version="1.0" encoding="utf-8"?>
<album type="basic" shape="square" orientation="vertical">
    <page width="414" height="414" type="frontCover">
        <sbackground color="0xFFFFFF" locked="false" />
        <pbackground source="" rotation="none" x="0" y="0" width="0" height="0" locked="false" />
        <images/>
        <frame source="" rotation="none" locked="false" />
        <shapes/>
        <texts/>
    </page>

    <page width="414" height="414" type="backCover" useEntirePage="true">
        <sbackground color="0xFFFFFF" locked="false" />
        <pbackground source="" rotation="none" x="0" y="0" width="0" height="0" locked="false" />
        <images/>
        <frame source="" rotation="none" locked="false" />
        <texts/>
        <shapes/>
    </page>
</album>';

$replaceXml = '<?xml version="1.0" encoding="utf-8"?>
<album type="savedCard" sides="double" shape="square" orientation="vertical">
    <page width="414" height="414" type="frontCover" useEntirePage="true" >
        <sbackground color="0xFFFFFF" />
        <pbackground source="/images/1-2800-1860-1358622465873.jpg" rotation="none" x="-108.9" y="0" width="648.82" height="431" transparency="1" flipped="false" mask_frame_name="" />
        <images>
        </images>
        <frame source="" />
        <shapes>
        </shapes>
        <texts>
        </texts>
    </page>
    <page width="414" height="414" type="leftPage" useEntirePage="true" >
        <sbackground color="0xCBCBFF" />
        <pbackground source="" rotation="none" x="0" y="0" width="0" height="0" transparency="1" flipped="false" mask_frame_name="" />
        <images>
        </images>
        <frame source="" />
        <shapes>
        </shapes>
        <texts>
        </texts>
    </page>
</album>';

接下来需要做的是:用第二个节点的相同节点的值替换第一个 XML 的节点之一。

我需要用 $replaceXml 的首页节点替换 $xml 中的首页节点。所以我需要在更换后拥有这个:

$xml = '<?xml version="1.0" encoding="utf-8"?>
<album type="basic" shape="square" orientation="vertical">
    <page width="414" height="414" type="frontCover" useEntirePage="true" >
        <sbackground color="0xFFFFFF" />
        <pbackground source="/images/1-2800-1860-1358622465873.jpg" rotation="none" x="-108.9" y="0" width="648.82" height="431" transparency="1" flipped="false" mask_frame_name="" />
        <images>
        </images>
        <frame source="" />
        <shapes>
        </shapes>
        <texts>
        </texts>
    </page>

    <page width="414" height="414" type="backCover" useEntirePage="true">
        <sbackground color="0xFFFFFF" locked="false" />
        <pbackground source="" rotation="none" x="0" y="0" width="0" height="0" locked="false" />
        <images/>
        <frame source="" rotation="none" locked="false" />
        <texts/>
        <shapes/>
    </page>
</album>';

最好的方法是什么?

我尝试了下一个方法

$xml = simplexml_load_string($xml);
$replaceXml = simplexml_load_string($replaceXml);
$xml->page[0] = $replaceXml->page[0];

但这似乎是错误的,因为我没有得到我需要的东西。

【问题讨论】:

    标签: php xml xml-parsing


    【解决方案1】:

    我找到了如何做到这一点:

    假设我们需要替换第一页

    $pageNum = 0;
    
    $xmlDoc = new DOMDocument();
    $xmlDoc->loadXML($xml);
    
    $replace = new DOMDocument();
    $replace->loadXML($replaceXml);
    
    $newNode = $replace->getElementsByTagName('page')->item($pageNum);
    $oldNode = $xmlDoc->getElementsByTagName('page')->item($pageNum);
    
    $newNode = $xmlDoc->importNode($newNode, true);
    $oldNode->parentNode->replaceChild($newNode, $oldNode);
    
    $xmlDoc->saveXML();
    

    根据需要工作

    【讨论】:

      猜你喜欢
      • 2014-08-27
      • 1970-01-01
      • 2012-01-04
      • 2018-03-05
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多