如何替换XML中的整个节点值

时间:2013-02-22 15:15:28

标签: php xml xml-parsing

我有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];

但似乎是错的,因为我得不到我需要的东西。

1 个答案:

答案 0 :(得分:0)

我发现了如何做到这一点:

假设我们需要替换第一页

$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();

根据需要运作