Adding a block of XML as child of a SimpleXMLElement object
Posted
by miCRoSCoPiC_eaRthLinG
on Stack Overflow
See other posts from Stack Overflow
or by miCRoSCoPiC_eaRthLinG
Published on 2009-12-01T07:32:54Z
Indexed on
2010/05/01
8:27 UTC
Read the original article
Hit count: 289
Hey all, I have this SimpleXMLElement object with a XML setup similar to the following...
$xml <<< EOX
<books>
<book>
<name>ABCD</name>
</book>
</books>
EOX;
$sx = new SimpleXMLElement( $xml );
Now I have a class named Book that contains info. about each book. The same class can also spit out the book info. in XML format akin the the above (the nested block).. example,
$book = new Book( 'EFGH' );
$book->genXML();
... will generate
<book>
<name>EFGH</name>
</book>
Now I'm trying to figure out a way by which I can use this generated XML block and append as a child of so that now it looks like... for example..
// Non-existent member method. For illustration purposes only.
$sx->addXMLChild( $book->genXML() );
...XML tree now looks like:
<books>
<book>
<name>ABCD</name>
</book>
<book>
<name>EFGH</name>
</book>
</books>
From what documentation I have read on SimpleXMLElement, addChild() won't get this done for you as it doesn't support XML data as tag value.
Any ideas on how I should go about this ?
Thanks, m^e
© Stack Overflow or respective owner