note 92004 deleted from ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Sat, 04 Jul 2009 16:45:08 +0000 |
| Subject: | note 92004 deleted from ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157919@lists.php.net to get a copy of this message | ||
Note Submitter: alab1001101 [at] goooo...ail.com
----
<?php
function xml2Tree( SimpleXMLElement $xml, $root = "/" ) {
$nodes = $xml->xpath($root.'*');
foreach( $nodes as $node ) {
$arr[] = array(
'tag' => $node->getName(),
'attributes' => current( $node->attributes() ),
'value' => xml2Tree( $node, null )
);
}
return ( empty( $arr ) ) ? (string) $xml : $arr;
}
$xml = '<root id="42">
<simple multi="yes" keep="yes">
String is kept as value
</simple>
<!-- Comments are lost -->
<complex keep="yes">
This string is lost
<simple keep="yes"/>
</complex>
</root>';
$xml = simplexml_load_string( $xml );
print_r( xml2Tree( $xml ) );
?>