如何使用php在xml文件中交换两个节点的位置

时间:2013-11-25 12:21:44

标签: php xml

有两个场景节点,我想交换改变它们的位置

我的对象根据此场景位置进行渲染,我想更改渲染顺序

提前感谢。

    <view hlookat="0" vlookat="0" fovtype="MFOV" fov="120" maxpixelzoom="2.0" fovmin="70" fovmax="140" limitview="range" vlookatmin="-56.738" vlookatmax="56.738"/>

    <preview url="panos/301760039505_20120929_7D_0891Panorama-e.tiles/preview.jpg"/>

    <image type="CUBE" multires="true" tilesize="512" progressive="false">
        <level tiledimagewidth="808" tiledimageheight="808">
            <cube url="panos/301760039505_20120929_7D_0891Panorama-e.tiles/%s/l1/%v/l1_%s_%v_%h.jpg"/>
        </level>
        <mobile>
            <cube url="panos/301760039505_20120929_7D_0891Panorama-e.tiles/mobile_%s.jpg"/>
        </mobile>
    </image>

    <!-- place your scene hotspots here -->

</scene>

<scene name="scene_301760039601_20120929_7D_0946Panorama-e" title="Exterior" onstart="" thumburl="panos/301760039601_20120929_7D_0946Panorama-e.tiles/thumb.jpg" lat="" lng="" heading="">

    <view hlookat="0" vlookat="0" fovtype="MFOV" fov="120" maxpixelzoom="2.0" fovmin="70" fovmax="140" limitview="range" vlookatmin="-56.738" vlookatmax="56.738"/>

    <preview url="panos/301760039601_20120929_7D_0946Panorama-e.tiles/preview.jpg"/>

    <image type="CUBE" multires="true" tilesize="512" progressive="false">
        <level tiledimagewidth="808" tiledimageheight="808">
            <cube url="panos/301760039601_20120929_7D_0946Panorama-e.tiles/%s/l1/%v/l1_%s_%v_%h.jpg"/>
        </level>
        <mobile>
            <cube url="panos/301760039601_20120929_7D_0946Panorama-e.tiles/mobile_%s.jpg"/>
        </mobile>
    </image>

    <!-- place your scene hotspots here -->

</scene>

1 个答案:

答案 0 :(得分:0)

将XML加载到DOMDocument中,使用Xpath或DOM函数查找节点并移动它们:

$xml = <<<'XML'
<scenes>
  <scene name="one"/>
  <scene name="other"/>
  <scene name="two"/>
</scenes>
XML;

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);

// find the nodes
$nodeOne = $xpath->evaluate('//scene[@name="one"]')->item(0);
$nodeTwo = $xpath->evaluate('//scene[@name="two"]')->item(0);

if (!$nodeOne->isSameNode($nodeTwo)) {

  // remember parent and position of the second node
  $parent = $nodeTwo->parentNode;
  $target = $nodeTwo->nextSibling;

  // move the second node
  $nodeOne->parentNode->insertBefore($nodeTwo, $nodeOne->nextSibling);

  // move the first node
  $parent->insertBefore($nodeOne, $target);
}
header('Content-type: text/xml');
echo $dom->saveXml();