我有 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];
但这似乎是错误的,因为我没有得到我需要的东西。