开发者

Merge entries in XMLfile (SimpleXML in PHP)

I have this in my XML file:

<product name="iphone">
    <variant name="iphone" product_number="12345" price="500" picture="iphone.jpg">
        <description><![CDATA[iphone]]></description>
        <short_description><![CDATA[]]></short_description>
        <deliverytime><![CDATA[]]></deliverytime>
        <options>
            <option group="Color" option="B开发者_JS百科lack" />
        </options>
    </variant>
</product>
<product name="iphone">
    <variant name="iphone" product_number="12345" price="500" picture="iphone.jpg">
        <description><![CDATA[iphone]]></description>
        <short_description><![CDATA[]]></short_description>
        <deliverytime><![CDATA[]]></deliverytime>
        <options>
            <option group="Color" option="White" />
        </options>
    </variant>
</product>

I want to merge it into this (Note that I merge the options tag):

<product name="iphone">
    <variant name="iphone" product_number="12345" price="500" picture="iphone.jpg">
        <description><![CDATA[iphone]]></description>
        <short_description><![CDATA[]]></short_description>
        <deliverytime><![CDATA[]]></deliverytime>
        <options>
            <option group="Color" option="Black" />
            <option group="Color" option="White" />
        </options>
    </variant>
</product>

Preferably I want to do it all in the memory since I will process it further afterwards.


that is not easy and i am looking for the same... one problem is to identicate which iphone is meant and so on.. There exists merge functions but in my case they did not work very well all: http://www.php.net/manual/de/ref.simplexml.php#92272 when you found no solution yet i think it is time to do it myself and report you:

Update 1: The functions xml_attribute and addxmlelement are from the normal php page (and i copied to the end). The function i wrote is very individually for one case and does not apply to all merging situation. i created a fiction for me.

XML: Neu (new to add)

<varrDaten>
        <person>
        <street id="adder">adder</street>
        <loc>land AAA</loc>
    </person>
    <person>
        <street id="exister">exister street</street>
        <loc>land gg</loc>
    </person>
    <person>
        <street id="updater">street is uptodate</street>
        <loc>land is updated</loc>
    </person>
</varrDaten>

XML (exists)

<varrDaten>
    <person>
        <street id="minuser">minuser</street>
        <loc>land 0</loc>
    </person>
    <person>
        <street id="exister">exister street</street>
        <loc>land lon</loc>
    </person>
    <person>
        <street id="updater">update need street</street>
        <loc>land need update</loc>
    </person>
</varrDaten>

function simplexml_merge2($xpOld,$xpNeu)
            {
            $selIDS=$xpNeu->xpath('//street[not(@id="0")]');
            foreach($selIDS as $ident)
                {
                //existiert in vorhandennen?
                $xpSelV=$xpOld->xpath('//person/street[@id="'.xml_attribute($ident,'id').'"]');
                if(count($xpSelV)>0)
                    {
                    echo "YES EXISTS".(string)$xpSelV[0][0]."<>".(string)$ident[0]."
"; //test of value maybe.. if(!((string)$xpSelV[0][0]==(string)$ident[0])) { //HERE YOU CAN ADD CRITERIA WHICH SHOULD BE SYCNRONISED IF ALREADY EXISTS echo "VAL not the same"; $xp2Ef=$xpOld->xpath('//person[./street[@id="'.xml_attribute($ident,'id').'"]]'); $xp2Ef[0][0]=(string)$ident[0]; } } else { echo "NOOO:".$xpNeu[0]; $xpEF=$xpOld->xpath('//varrDaten'); echo '//person[./street[@id="'.xml_attribute($ident,'id').'"]]'; $xp2Ef=$xpNeu->xpath('//person[./street[@id="'.xml_attribute($ident,'id').'"]]'); AddXMLElement($xpEF[0],$xp2Ef[0]); } } }
Add. func
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
    else
        return false;
}
 function AddXMLElement(SimpleXMLElement $dest, SimpleXMLElement $source)
    {
        $new_dest = $dest->addChild($source->getName(), $source[0]);

    foreach ($source->attributes() as $name => $value)
    {
        $new_dest->addAttribute($name, $value);
    }

    foreach ($source->children() as $child)
    {
        AddXMLElement($new_dest, $child);
    }
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜