note 96317 added to class.simplexmlelement
| From: | phpatkeithtylerdotcom at osu1 dot php dot net | Date: | Sat, 20 Feb 2010 02:23:19 +0000 |
| Subject: | note 96317 added to class.simplexmlelement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166203@lists.php.net to get a copy of this message | ||
The root node element of your input XML string is not retrievable as a property.
<?php
$xml="<foo>bar</foo>";
$sxe=new SimpleXMLElement($xml);
print $sxe->foo;
?>
prints nothing. You can only get to the root element via the array index method ($sxe[0]).
Also, you may not have two (or more) root elements -- that is apparently not well-formed XML.
<?php
$xml="<foo/><bar/>";
$sxe=new SimpleXMLElement($xml);
?>
throws an exception. A Q&D is to append an arbitraty root node structure to both ends of the
input:
<?php
$xml="<foo/><bar/>";
$sxe=new SimpleXMLElement("<z>".$xml."</z>");
?>
Doing this also solves the above problem of root node property accessibility. (It may not work if
your XML string includes a declaration.)
----
Server IP: 64.71.164.2
Probable Submitter: 174.46.204.210
----
Manual Page -- http://www.php.net/manual/en/class.simplexmlelement.php
Edit -- https://master.php.net/note/edit/96317
Del: integrated -- https://master.php.net/note/delete/96317/integrated
Del: useless -- https://master.php.net/note/delete/96317/useless
Del: bad code -- https://master.php.net/note/delete/96317/bad+code
Del: spam -- https://master.php.net/note/delete/96317/spam
Del: non-english -- https://master.php.net/note/delete/96317/non-english
Del: in docs -- https://master.php.net/note/delete/96317/in+docs
Del: other reasons-- https://master.php.net/note/delete/96317
Reject -- https://master.php.net/note/reject/96317
Search -- https://master.php.net/manage/user-notes.php