views:

23

answers:

3

Hello,

I'm using simplexml_load_file($path_xml_file), it works good!

If I do print_r($xml) it prints:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [Message] => Login Successful
            [Token] => 11111111111
            [DataFormat] => CSV

        )
)

How Can I get "token" value?

Thank you!

A: 
$xml = <<<XML
<root>
<elem attrib="value" />
</root>
XML;

$sxml = simplexml_load_string($xml);
$attrs = $sxml->elem->attributes();
echo $attrs["attrib"]; //or just $sxml->elem["attrib"]

Use SimpleXMLElement::attributes.

Truth is, the SimpleXMLElement get_properties handler lies big time. There's no property named "@attributes", so you can't do $sxml->elem->{"@attributes"}["attrib"].

Artefacto
+1  A: 

You can just do:

echo strval($xml['token']);
Alix Axel
A: 

To Alix's point, you can do:

echo strval($xml['token');

though it's a bit quicker to do:

echo (string)$xml['token'];

If you're looking for a list of these attributes though, XPath will be your friend

print_r($xml->xpath('@token'));
Cory Collier