Reputation: 226
Before someone points out that there are a ton of similar questions like this, please keep in mind I have tried and exhausted all methods I could find here on stacked.
I'm having trouble using simplexml to pull out the data I want from a response that is structured like this.
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:body>
<authenticateresponse xmlns="http://somesite.co.nz">
<authenticateresult>
<username>Username</username>
<token>XXXXXXXXX</token>
<reference>
<message>Access Denied</message>
</reference>
</authenticateresult>
</authenticateresponse>
</soap:body>
In this case I'd like to know how to pull out the token and username.
Upvotes: 1
Views: 30
Reputation: 89285
Your XML has default namespace declared at authenticateresponse
element :
xmlns="http://somesite.co.nz"
Notice that the element where default namespace is declared along with the descendant elements without prefix are in the same namespace. To access element in default namespace, you need to map a prefix to the namespace URI and use the prefix in the XPath, for example :
$raw = <<<XML
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:body>
<authenticateresponse xmlns="http://somesite.co.nz">
<authenticateresult>
<username>Username</username>
<token>XXXXXXXXX</token>
<reference>
<message>Access Denied</message>
</reference>
</authenticateresult>
</authenticateresponse>
</soap:body>
</soap:envelope>
XML;
$xml = new SimpleXMLElement($raw);
$xml->registerXPathNamespace('d', 'http://somesite.co.nz');
$username = $xml->xpath('//d:username');
echo $username[0];
output :
Username
A few former related questions :
Upvotes: 1