append child to simple_xml_string

Is there a way to append a child to a xml string.

For example, I have:

$Element = simplexml_load_string('<tree></tree>');

I want to append a child to the node "tree".
pillmillAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Beverley PortlockConnect With a Mentor Commented:
<?php

$Element = simplexml_load_string('<tree></tree>');
$Element->addChild("fred");

echo htmlentities( $Element->asXml() );

0
 
Beverley PortlockCommented:
0
 
Ray PaseurCommented:
This will not only add the child node, but it will give it some content.
<?php // RAY_temp_pillmill.php

$obj = simplexml_load_string('<tree></tree>');
$obj->addChild("fred");
$obj->fred = 'Flintstone';

echo htmlentities( $obj->asXml() );

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.