0

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

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

4

1 回答 1

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

根据需要工作

于 2013-02-23T16:25:08.820 回答