tags:

views:

374

answers:

1

I have a SimpleXMLElement object $child, and a SimpleXMLElement object $parent.

How can I add $child as a child of $parent? Is there any way of doing this without converting to DOM and back?

The addChild() method only seems to allow me to create a new, empty element, but that doesn't help when the element I want to add $child also has children. I'm thinking I might need recursion here.

+1  A: 

I know this isn't the most helpful answer, but especially since you're creating/modifying XML, I'd switch over to using the DOM functions. SimpleXML's good for accessing simple documents, but pretty poor at changing them.

If SimpleXML is treating you kindly in all other places and you want to stick with it, you still have the option of jumping over to the DOM functions temporarily to perform what you need to and then jump back again, using dom_import_simplexml() and simplexml_import_dom(). I'm not sure how efficient this is, but it might help you out.

nickf
Fortunately it's early enough that I won't waste too much time by converting the whole thing to DOM (probably take an hour). I think this is the solution I'll probably end up with, unless someone else has a really amazing idea.Thanks for the answer!
thomasrutter