问题
I am trying to get all the elements and values as array from the below soap response. But I am only getting elements but no values.
I have used this parser to get the elements and values. Thanks in advance.
Here is the code I am using,
$response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<toy:SearchReq xmlns:toy="www.something.com/toy" xmlns:gen="www.something.com/gen">
<gen: manufacturedIn="SomePlace"/>
<toy:SearchToy>
<toy:SearchType>
<gen:Shop Code="WALMART"/>
</toy:SearchType>
</toy:SearchToy>
<toy:SearchToy>
<toy:SearchType>
<gen:Shop Code="AMAZON"/>
</toy:SearchType>
</toy:SearchToy>
</toy:SearchReq>
</soapenv:Body>
</soapenv:Envelope>';
$doc = simplexml_load_string($response);
$path = array($doc
->children('http://schemas.xmlsoap.org/soap/envelope/')
->Body
->children('www.something.com/toy'));
foreach($path as $elem){
print_r($elem);
$elemChild = $elem->children('www.something.com/gen');
var_dump($elemChild);
}
回答1:
You can read SOAP messages by registering the namespace - you only need the namespace bit if you care about the SOAP parts of the message.
$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//toy') as $toy)
{
print_r($toy);
}
回答2:
See complete reference here: https://stackoverflow.com/a/38783380/6511851
$sxe = new SimpleXMLElement($response); //in $response variable it's an XML file or response
$sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
$result = $sxe->xpath("//NewDataSet");
echo "<pre>";
foreach ($result[0] as $title) {
print_r($title);
}
For more information visit site: https://vaja906programmingworld.wordpress.com/
来源:https://stackoverflow.com/questions/19103575/parse-soap-response-using-php