使用 php 和 simplexml 重新排序 XML 节点 [英] Reorder XML nodes with php and simplexml

查看:59
本文介绍了使用 php 和 simplexml 重新排序 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面当前正在更新现有的 xml,问题是当它添加新节点时,它们会转到 xml 或父标签的末尾,即:

My page is currently updating an existing xml, the problem is that when it adds new nodes they go to the end of the xml or parent tag ie:

<N1></N1>
<N1></N1>
<N2></N2>
<N2></N2>
<N2></N2>
<N1></N1>  //I want this node to be displayed with the other N1 nodes.

我已经阅读了不使用 simplexml 的解决方案,但问题是我已经编写了围绕 simplexml 进行更改的许多代码的方法.此外,请记住,该页面不会从头开始重写 xml,而只是更改不同的内容.那么我问的可能吗?谢谢.

I have read solutions for this that don't use simplexml, but the problem is that I have already written way to much code around simplexml to change now. Also, keep in mind that the page isn't rewriting the xml from scratch, rather it is simply changing what is different. So is what I'm asking possible? thanks.

推荐答案

据我所知,无法使用 SimpleXML 对节点重新排序.您最好的选择可能是将其转换为 DOMDocument 对象,然后使用 DOMNode->insertBefore() 方法在另一个 DOMNode 之前插入一个新的 DOMNode.

There's no way to re-order nodes using SimpleXML that I know of. Your best option may be to convert it to a DOMDocument object and then use the DOMNode->insertBefore() method to insert a new DOMNode before another one.

您想要做的一个很好的例子可能是 http://us.php.net/manual/en/function.dom-import-simplexml.php.

A good example of what you're looking to do may be http://us.php.net/manual/en/function.dom-import-simplexml.php.

这篇关于使用 php 和 simplexml 重新排序 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆