jreed121
jreed121

Reputation: 2097

Reorder XML nodes with php and simplexml

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.

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.

Upvotes: 1

Views: 1964

Answers (1)

Sarah
Sarah

Reputation: 513

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.

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

Upvotes: 7

Related Questions