Re: SimpleXML Class

From: Date: Thu, 07 May 2009 02:06:32 +0000
Subject: Re: SimpleXML Class
References: 1 2 3 4 5 6 7  Groups: php.general php.general 
Request: Send a blank email to php-general+get-292370@lists.php.net to get a copy of this message
Cesco wrote:
Could you help me clarify one thing that I don't understand... let's put it simple, just imagine that I have a tiny XML document with a list of movies: <movies>
    <title>
        <i>Gone</i> with <b>the</b> wind
    </title>
</movies> I want to read this XML file and write the name of the first (and only) movie in the list; for this reason I have choose to use SimpleXML since it was looking quite user-friendly. But there's a thing I don't understand... when I have some children, how do I understand which is the first child and which is the last ? I have tried to write this, but I'm getting a wrong result: instead of "Gone with the wind" I got "with wind Gone the", because I understand that the tag <title> contains all the text that is not formatted, and then it writes all the children of <title>: <i>Gone</i> and <b>the</b> <?php
    $xml = new SimpleXMLElement("<movies><title><i>Gone</i> with <b>the</b> wind</title></movies>");
        echo ($xml->title . " ");
    foreach ($xml->title->children() as $element) {
        echo ($element . " ");
    }
        // Returns "with wind Gone the"
    ?>
I'm using PHP 5.2.5, could you tell me what am I doing wrong ? Thank you
cdata <movies>
    <title>
        <![CDATA[<i>Gone</i> with <b>the</b> wind]]>
    </title>
</movies>

« previous php.general (#292370) next »