note 40361 added to ref.simplexml
| From: | ash at bigfoot dot de | Date: | Mon, 01 Mar 2004 12:20:50 +0000 |
| Subject: | note 40361 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66039@lists.php.net to get a copy of this message | ||
I had problems to use SimpleXML with OpenOffice's content.xml.
Although
print_r($simple_xml);
printed the whole XML object
simplexml_element Object
(
[script] => simplexml_element Object
(
)
[font-decls] => simplexml_element Object
(
[font-decl] => Array
...
all first level object vars (script, font-decls, automatic-styles, body) contained nothing if I
tried to access them:
print_r($simple_xml->{'font-decls'});
returned
simplexml_element Object
(
)
and direct access to a deeper value returns NULL:
echo(gettype($simple_xml->{'font-decls'}->{'font-decl'}));
So I wrote a function to turn the whole object into an array, maybe you find it useful.
function &simpleXMLtoArray(&$simple_xml) {
$xml_array = (array)$simple_xml;
$result = array();
foreach ($xml_array as $key => $value) {
if(is_object($value) || is_array($value))
$result[$key]=&simpleXMLtoArray($value);
else
$result[$key]=$value;
}
return $result;
}
print_r(simpleXMLtoArray($simple_xml));
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40361
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40361&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40361&report=yes
Search -- http://master.php.net/manage/user-notes.php