Viewed   69 times

The "addChild" method of SimpleXMLElement seems like it should be the right choice, but it apparently only takes strings representing the tagname of the new child.

There's the object-ish notation for referencing nodes of the tree and setting them, e.g. $simpleXMLNode->child = value, but that only seems to work for simple text/numeric values. If I try the following:

$s = new SimpleXMLElement('<root/>');
$t = new SimpleXMLElement('<child/>');
$s->a = $t;
echo $s->asXML()

I get:

<?xml version="1.0"?>

when I was hoping for:

<?xml version="1.0"?>

I thought of converting $t to a string and then adding it (after stripping out the XML declaration):

$s->a = substr($t->asXML(),22)

But this yields:

<?xml version="1.0"?>

Again, not what I was hoping for.

Is there a typical way to accomplish this kind of thing with SimpleXML?



Hey unknown. You have to use the DOMElement interface to your SimpleXML objects to achieve this.


$s = new SimpleXMLElement('<root/>');
$t = new DOMElement('child');

$dom = dom_import_simplexml($s);

echo $s->asXML();
// <root><child/></root>

If you need more specific details, let me know. There are several examples in the documentation and comments for the dom_import_simplexml() method too:

Tuesday, December 20, 2022
if($A->b->c != null) //c exists

If c does not exist, its value will be null (or, to be more precise, it will have no value). Note, however, that for this to work, both A and b need to not be null. Otherwise, PHP will throw an error (I think).

Saturday, December 24, 2022

Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so

$value = (string) $xml->someNode->innerNode;
Monday, August 15, 2022

The Visual Studio Development Server is codenamed Cassini.

From ASP.NET 2.0: A Getting Started Guide

Cassini doesn't support virtual directories, security settings, 
or any of IIS's other fancy features; it's just a very simple web server 
that gives you the basics you need to get up and running.

I am using the IIS which is included with Windows XP Pro. It only allows one website without tweaking but does have virtual directories. If you are on Vista, you can setup multiple sites in IIS from what I have heard.

Sunday, October 2, 2022

It's pretty simple to customize your app tab favicons.
Because the app tab favicon is generated by the bookmark just...
(1) install this --
(2) view your bookmarks, right click and select the custom image you want
(3) restart browser (for new image to "take")

Wednesday, December 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :