如何在php中读取soapenv:Envelope xml节点?

如何在php中读取soapenv:Envelope xml节点?,php,xml,Php,Xml,我在下面给出了xml数据文件。我想读节点。有人能帮我吗 $this->soapResponse 具有响应xml数据 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <addItemTo

我在下面给出了xml数据文件。我想读节点。有人能帮我吗

$this->soapResponse 
具有响应xml数据

 <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<addItemToShoppingCartResponse xmlns="http://www.sample.com/services">
<ack>Warning</ack>
</addItemToShoppingCartResponse>
</soapenv:Body>
</soapenv:Envelope>

如果要使用SimpleXML,则必须使用名称空间和
子类
方法:

echo (string) $xml->children("http://schemas.xmlsoap.org/soap/envelope/")
    ->Body
    ->children()
    ->addItemToShoppingCartResponse
    ->ack;
输出:

Warning

您可能希望使用
simplexml\u load\u string()
,然后使用
registerxpathnespace()
注册名称空间,然后可以开始使用
xpath()
来寻址项目。 然后您就可以使用这些名称空间来正确地处理xpath查询中的项

<?php

$xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<addItemToShoppingCartResponse xmlns="http://www.sample.com/services">
    <ack>Warning</ack>
</addItemToShoppingCartResponse>
</soapenv:Body>
</soapenv:Envelope>
';

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");

// register your used namespace prefixes
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('services', 'http://www.sample.com/services'); // ? ns not in use

// then use xpath to adress the item you want (using this NS)
$nodes = $xml->xpath('/soapenv:Envelope/soapenv:Body/services:addItemToShoppingCartResponse/services:ack');
$ack = (string) $nodes[0];
var_dump($ack);

看看这个,;我想读取这个xml文件。我得到了回应。这不是请求xml。无论谁关闭了它:这个问题是关于如何在soap响应中寻址数据项,而不是关于请求内容。感谢您的回复。如果我在TophingCartResponse节点中添加了子节点(我的意思是如何应用foreach循环)warrning 231 231我已经添加了问题的答案。
<?php

$xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<addItemToShoppingCartResponse xmlns="http://www.sample.com/services">
    <ack>Warning</ack>
</addItemToShoppingCartResponse>
</soapenv:Body>
</soapenv:Envelope>
';

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");

// register your used namespace prefixes
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('services', 'http://www.sample.com/services'); // ? ns not in use

// then use xpath to adress the item you want (using this NS)
$nodes = $xml->xpath('/soapenv:Envelope/soapenv:Body/services:addItemToShoppingCartResponse/services:ack');
$ack = (string) $nodes[0];
var_dump($ack);
// example data looks like this
// addItemToShoppingCartResponse -> ship -> item
$xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<addItemToShoppingCartResponse xmlns="http://www.sample.com/services">
   <ack>warrning</ack> 
   <ship>
     <item>231</item>
     <item>232</item>
   </ship>
</addItemToShoppingCartResponse>
</soapenv:Body>
</soapenv:Envelope>
';

// load xml like above + register NS

// select items node (addItemToShoppingCartResponse -> ship)
$items = $xml->xpath('/soapenv:Envelope/soapenv:Body/services:addItemToShoppingCartResponse/services:ship/services:item');

// iterate item nodes
foreach ($items as $item) 
{
  //var_dump($item);
  echo (string) $item[0];
}