0

我想在下面给定的 XML 中添加一个节点,我想在数据节点中添加一个项目,如下所示,项目 id 递增,我想使用 PHP 执行此操作

<?xml version="1.0" encoding="utf-8"?>
<data>
    <item id="1" order="1">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_01.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_01.jpg]]></item>
            <item propertyType="3"><![CDATA[true]]></item>
            <item propertyType="4"><![CDATA[openPopup;id=1]]></item>
        </properties>
    </item>

    <item id="2" order="2">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_02.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_02.jpg]]></item>
            <item propertyType="3"><![CDATA[false]]></item>
            <item propertyType="4"><![CDATA[]]></item>
        </properties>
    </item>

    <item id="3" order="3">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_03.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_03.jpg]]></item>
            <item propertyType="3"><![CDATA[false]]></item>
            <item propertyType="4"><![CDATA[]]></item>
        </properties>
    </item>
</data>

谁能帮我

4

1 回答 1

0

simplexml_insert_after从这里获取的函数: PHP SimpleXML:在特定位置插入节点

<?php
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>
<data>
    <item id="1" order="1">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_01.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_01.jpg]]></item>
            <item propertyType="3"><![CDATA[true]]></item>
            <item propertyType="4"><![CDATA[openPopup;id=1]]></item>
        </properties>
    </item>

    <item id="2" order="2">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_02.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_02.jpg]]></item>
            <item propertyType="3"><![CDATA[false]]></item>
            <item propertyType="4"><![CDATA[]]></item>
        </properties>
    </item>

    <item id="3" order="3">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_03.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_03.jpg]]></item>
            <item propertyType="3"><![CDATA[false]]></item>
            <item propertyType="4"><![CDATA[]]></item>
        </properties>
    </item>
</data>');

function simplexml_insert_after(SimpleXMLElement $insert, SimpleXMLElement $target)
{
    $target_dom = dom_import_simplexml($target);
    $insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
    if ($target_dom->nextSibling) {
        return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
    } else {
        return $target_dom->parentNode->appendChild($insert_dom);
    }
}

foreach($xml->xpath('/data/item') as $node) {
    $id = (int)$node['id'];
}

$next_id = $id + 1;


simplexml_insert_after(new SimpleXMLElement('<item id="'.$next_id.'" order="'.$next_id.'">
        <properties>
            <item propertyType="1"><![CDATA[images/gallery/images/bg_ph_03.jpg]]></item>
            <item propertyType="2"><![CDATA[images/gallery/thumbs/small_pic_03.jpg]]></item>
            <item propertyType="3"><![CDATA[false]]></item>
            <item propertyType="4"><![CDATA[]]></item>
        </properties>
    </item>'), $xml);

var_dump($xml->asXML());
于 2013-03-26T18:26:49.807 回答