0

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?

2 Answers 2

2

something like this:

$xml  = simplexml_load_file($page);
foreach ($xml->FUNDAMENTALS->FUNDAMENTAL as $fundamental) {
    $symbol = $fundamental['Symbol'];
    $name = $fundamental['Name'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

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...
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.