rramiii
rramiii

Reputation: 1186

how can I get value in SimpleXMLElement object

Thanx for reading. I have an simplexmlElement object. I get this result:

SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [key] => memberCard [value] => memberCard ) [1] => SimpleXMLElement Object ( [key] => AuthToken [value] => Auth-Token ) ) ) 

when I use this code:

$xml=simplexml_load_file('get.xml');
print_r($xml->clientSide[0]->item);

Now I'm trying to get key value but I can't, I tried

print_r($xml->clientSide[0]->item[0]->item[0]->key);

but the result was:

SimpleXMLElement Object ( [0] => memberCard )

I don't know how to get key node value. The original xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:mapping xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:IM-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <serverSide xsi:type="IM-ENC:Array"></serverSide>
    <clientSide xsi:type="IM-ENC:Array">
        <item xsi:type="ns2:Map">
            <item>
                <key xsi:type="xs:string">memberCard</key>
                <value xsi:type="xs:string">memberCard</value>
            </item>
            <item>
                <key xsi:type="xs:string">AuthToken</key>
                <value xsi:type="xs:string">Auth-Token</value>
            </item>
        </item>
    </clientSide>
</xs:mapping>

Any help or ideas would be greatly appreciated.

Upvotes: 1

Views: 238

Answers (2)

You can use a foreach to loop over all the key-value pairs..

foreach ($xml->clientSide->item->item as $k=>$child)
{
    echo $child->key;
    echo "<br>";
    echo $child->value;
    echo "<br><br>";
}

Working Demo

Upvotes: 0

hindmost
hindmost

Reputation: 7195

You have to do explicit conversion to string:

print_r((string)$xml->clientSide[0]->item[0]->item[0]->key);

Thereby you call the method SimpleXMLElement::__toString

Upvotes: 1

Related Questions