使用带有索引位置的 addChild
当我添加子节点时,如何根据它的兄弟节点选择它的位置?
下面是一个示例:
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道 Artefacto 的答案 已被接受,但看起来做比需要的更多的工作。一个更简单的替代方案,使用他的函数签名,可能类似于下面的函数。
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.
您必须回退到 DOM 并使用 dom_import_simplexml。
You have to fallback to DOM and use
dom_import_simplexml
.