Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: newbie question for XML.. why array == 1st element

From: petersprc <petersprc@-----.--->
To: NULL
Date: 2/29/2008 11:52:00 PM
Hi,

This is because simple XML nodes are stored internally as objects in a
linked list.

When you use the arrow (->) operator, the first matching child is
returned by the read_property object handler.

When all of a node's properties are fetched as a result of executing
print_r, children with the same name are combined into an array by the
get_properties object handler.

SimpleXMLElement implements the Travesable interface, which allows you
to iterate over these objects using foreach.

More info on object handler functions is here:
http://www.php-mag.net/magphpde/magphpde_article/psecom,id,382,nodeid,21.html

Regards,

John Peters

On Feb 29, 10:16 pm, Summercool <Summercooln...@gmail.com> wrote:
> it seems quite strange that the array also prints out as element 1 in
> the array in the following:
>
> the second print_r() statement... printing out an array actually
> prints out the first element...
>
> Code:
> ========================================================================
> <pre>
> <?php
>
> $xml =<<<XML
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <notes>
>     <note>
>         <to>Tove</to>
>         <from>Jani</from>
>         <heading>Reminder</heading>
>         <body>Don't forget this weekend!</body>
>     </note>
>     <note>
>         <to>Tove2</to>
>         <from>Jani2</from>
>         <heading>Reminder2</heading>
>         <body>Don't forget this weekend2!</body>
>     </note>
> </notes>
>
> XML;
>
> $xml = new SimpleXMLElement($xml);
>
> print_r($xml);
> print_r($xml->note);
> print_r($xml->note[0]);
> print_r($xml->note[1]);
>
> echo "\n\nlooping\n\n";
> foreach($xml->note as $note)
>     print_r($note);
>
> ?>
>
> Output:
> ========================================================================
> SimpleXMLElement Object
> (
>     [note] => Array
>         (
>             [0] => SimpleXMLElement Object
>                 (
>                     [to] => Tove
>                     [from] => Jani
>                     [heading] => Reminder
>                     [body] => Don't forget this weekend!
>                 )
>
>             [1] => SimpleXMLElement Object
>                 (
>                     [to] => Tove2
>                     [from] => Jani2
>                     [heading] => Reminder2
>                     [body] => Don't forget this weekend2!
>                 )
>
>         )
>
> )
> SimpleXMLElement Object
> (
>     [to] => Tove
>     [from] => Jani
>     [heading] => Reminder
>     [body] => Don't forget this weekend!
> )
> SimpleXMLElement Object
> (
>     [to] => Tove
>     [from] => Jani
>     [heading] => Reminder
>     [body] => Don't forget this weekend!
> )
> SimpleXMLElement Object
> (
>     [to] => Tove2
>     [from] => Jani2
>     [heading] => Reminder2
>     [body] => Don't forget this weekend2!
> )
>
> looping
>
> SimpleXMLElement Object
> (
>     [to] => Tove
>     [from] => Jani
>     [heading] => Reminder
>     [body] => Don't forget this weekend!
> )
> SimpleXMLElement Object
> (
>     [to] => Tove2
>     [from] => Jani2
>     [heading] => Reminder2
>     [body] => Don't forget this weekend2!
> )
>
> ------------------------------------------------------------------------
> if you are egoistic and self-righteous, thinking any other behavior
> unlike your own is stupid, please do not reply to this post, or
> your posting will be ignored.
> ------------------------------------------------------------------------



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent