views:

79

answers:

2

Hi guys,

How can I choose it's position based on it's sibblings when I add a child node ?

Here is an example :

<?php
    $_XML = '   <Test>
                    <Menu>
                        <Link href="page1.htm" />
                        <Link href="page2.htm" />
                        <Link href="page4.htm" />
                    </Menu>
                </Test>';

    $_RenderedXML = new SimpleXMLElement($_XML);

    //Add a new Link node
    $_NewLink = $_RenderedXML->Menu->addChild("Link");
    $_NewLink->addAttribute("href", "page3.htm");

    echo $_RenderedXML->asXML();
?>

This will render the XML with the new node below it's sibbling. I would like to specify it's position.

addChild("Link", 2) //or something.

Any guess ?

+1  A: 

You have to fallback to DOM and use dom_import_simplexml.

$_RenderedXML = new SimpleXMLElement($_XML);

//Add a new Link node
addchild_at($_RenderedXML->Menu, "Link", 2);
$_RenderedXML->Menu->Link[2]->addAttribute("href", "page3.htm");


function addchild_at(SimpleXMLElement $sxml, $tagname, $i) {
    $elem = dom_import_simplexml($sxml);
    $new = $elem->ownerDocument->createElement($tagname);
    $chnodes = array();
    foreach ($elem->childNodes as $cn) {
        $chnodes[] = $cn;
    }
    $chnodes = array_filter($chnodes,
        function (DOMNode $dn) { return $dn->nodeType == XML_ELEMENT_NODE; }
    );
    $chnodes = array_values($chnodes);

    if ($i < count($chnodes))
        $elem->insertBefore($new, $chnodes[$i]);
    else
        $elem->appendChild($new);
}
Artefacto
Awesome! It's working as expected.
Cybrix
+1  A: 

I know that Artefacto's answer has been accepted already but it looks to be doing more work than is required. A simpler alternative, using his function signature, could look like the function below.

function addchild_at(SimpleXMLElement $sxml, $tagname, $i)
{
    $parent = dom_import_simplexml($sxml);
    $child  = $parent->ownerDocument->createElement($tagname);
    $target = $parent->getElementsByTagname('*')->item($i);
    if ($target === NULL) {
        $parent->appendChild($child);
    } else {
        $parent->insertBefore($child, $target);
    }
}
salathe
Nice, I didn't know you could retrieve all the elements with `getElementsByTagname('*')`.
Artefacto
@Artefacto, now you do. :-)
salathe
Great! I like how simplier this function is. What should I do now?
Cybrix