-2

我试图从下面的肥皂响应中获取所有元素和值作为数组。但我只得到元素但没有价值。

我已经使用这个解析器来获取元素和值。提前致谢。

这是我正在使用的代码,

  $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);
}
4

2 回答 2

0

您可以通过注册名称空间来读取 SOAP 消息 - 如果您关心消息的 SOAP 部分,则只需要名称空间位。

$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//toy') as $toy)
{
    print_r($toy);
}
于 2013-09-30T22:12:30.597 回答
0

在此处查看完整参考: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);
}

欲了解更多信息,请访问网站:https ://vaja906programmingworld.wordpress.com/

于 2016-08-05T08:58:53.927 回答