note 80855 modified in ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:15:36 +0000 |
| Subject: | note 80855 modified in ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155504@lists.php.net to get a copy of this message | ||
As was said before don't use var_dump() or print_r() to see SimpleXML object structure as they
do not returns always what you expect.
Consider the following:
<?php
// data in xml
$xml_txt = '
<root>
<folder ID="65" active="1"
permission="1"><![CDATA[aaaa]]></folder>
<folder ID="65" active="1"
permission="1"><![CDATA[bbbb]]></folder>
</root>';
// load xml into SimpleXML object
$xml = simplexml_load_string($xml_txt, 'SimpleXMLElement',
LIBXML_NOCDATA);//LIBXML_NOCDATA LIBXML_NOWARNING
// see object structure
print_r($xml);
/* this prints
SimpleXMLElement Object
(
[folder] => Array
(
[0] => aaaa
[1] => bbbb
)
)
*/
// but...
foreach ($xml->folder as $value){
print_r($value);
}
/* prints complete structure of each folder element:
SimpleXMLElement Object
(
[@attributes] => Array
(
[ID] => 65
[active] => 1
[permission] => 1
)
[0] => aaaa
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[ID] => 65
[active] => 1
[permission] => 1
)
[0] => bbbb
)
*/
?>
--was--
As was said before don't use var_dump() or print_r() to see SimpleXML object structure as they
do not returns always what you expect.
Consider the following:
<?php
// data in xml
$xml_txt = '
<root>
<folder ID="65" active="1"
permission="1"><![CDATA[aaaa]]></folder>
<folder ID="65" active="1"
permission="1"><![CDATA[bbbb]]></folder>
</root>';
// load xml into SimpleXML object
$xml = simplexml_load_string($xml_txt, 'SimpleXMLElement',
LIBXML_NOCDATA);//LIBXML_NOCDATA LIBXML_NOWARNING
// see object structure
print_r($xml);
/* this prints
SimpleXMLElement Object
(
[folder] => Array
(
[0] => aaaa
[1] => bbbb
)
)
*/
// but...
foreach ($xml->folder as $value){
print_r($value);
}
/* prints complete structure of each folder element:
SimpleXMLElement Object
(
[@attributes] => Array
(
[ID] => 65
[active] => 1
[permission] => 1
)
[0] => aaaa
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[ID] => 65
[active] => 1
[permission] => 1
)
[0] => bbbb
)
*/
?>
http://php.net/manual/en/ref.simplexml.php