Using addChild with an index position

2019-07-16 09:25发布

问题:

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.

回答1:

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);
    }
}


回答2:

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);
}