views:

40

answers:

2

Hello everybody!

I download a webpage with a XML code:

$xml  = simplexml_load_file($page);

The results are:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [Message] => Success
            [Date] => 0001-01-01T00:00:00
        )

    [FUNDAMENTALS] => SimpleXMLElement Object
        (
            [FUNDAMENTAL] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [Symbol] => AAA
                                    [Name] => Description AAA

                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [Symbol] => BBB
                                    [Name] => Description BBB

                                )

                        )
                )
        )
)

I have to read each [Symbol] [Name], how can I do?

Thank you very much!

+2  A: 

something like this:

$xml  = simplexml_load_file($page);
foreach ($xml->FUNDAMENTALS->FUNDAMENTAL as $fundamental) {
    $symbol = $fundamental['Symbol'];
    $name = $fundamental['Name'];
}
kgb
Yup this is the easiest.Hooray for Array-acces.Beware of datatypes (having arrays in members or vice-versa). You could recast them to array...
Deefjuh
A: 

You can also use the SimpleXmlIterator:

The SimpleXMLIterator provides recursive iteration over all nodes of a SimpleXMLElement object.

If you only want to iterate over the <FUNDAMENTAL> elements, you might want to consider fetching them with an XPath, e.g.

$fundamentals = $sxe->xpath('//FUNDAMENTAL');

If you provide some XML to your question, I will provide you with a code example.

Gordon