PHP高级XML添加到项目节点

时间:2013-03-26 18:16:24

标签: php xml-parsing xml-serialization

我想在下面给出的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>

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

从此处获取的函数simplexml_insert_afterPHP SimpleXML: insert node at certain position

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