使用带有索引位置的 addChild

发布于 2024-09-13 04:25:41 字数 719 浏览 3 评论 0原文

当我添加子节点时,如何根据它的兄弟节点选择它的位置?

下面是一个示例:

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

这将使用其同级节点下方的新节点来呈现 XML。 我想明确它的位置。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-09-20 04:25:41

我知道 Artefacto 的答案 已被接受,但看起来做比需要的更多的工作。一个更简单的替代方案,使用他的函数签名,可能类似于下面的函数。

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

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);
    }
}
雪化雨蝶 2024-09-20 04:25:41

您必须回退到 DOM 并使用 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文