SimpleXML 解析问题
所有,
尝试解析此 SOAP 响应,但 xpath() 返回
调试警告:SimpleXMLElement::xpath() 未定义的命名空间前缀 调试警告:SimpleXMLElement::xpath() 评估失败
$result = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<env:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<v4:TrackReply xmlns:v4="http://fedex.com/ws/track/v4">
...
</v4:TrackReply>
</env:Body>
</soapenv:Envelope>';
$xml = simplexml_load_string($result,NULL,NULL,'http://schemas.xmlsoap.org/soap/envelope/');
foreach($xml->xpath('env:Body') as $body){
//drill down here...
}
All,
Trying to parse this SOAP response but xpath() is returning
Debug Warning: SimpleXMLElement::xpath() Undefined namespace prefix
Debug Warning: SimpleXMLElement::xpath() evaluation failed
$result = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<env:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<v4:TrackReply xmlns:v4="http://fedex.com/ws/track/v4">
...
</v4:TrackReply>
</env:Body>
</soapenv:Envelope>';
$xml = simplexml_load_string($result,NULL,NULL,'http://schemas.xmlsoap.org/soap/envelope/');
foreach($xml->xpath('env:Body') as $body){
//drill down here...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将命名空间注册到 'env'
$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
编辑
此代码设置
body
中子元素的命名空间。或者,您可以直接使用以下命令获取类似
TrackReply
的元素:Try registering the namespace to 'env'
$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
Edit
This code sets the namespace of child elements within
body
Alternatively, you can get an element like
TrackReply
directly with:SimpleXML 忽略 XML 结构的
env:
部分,而是返回如下构造的对象:您可以使用以下 XPath 查询访问正文:
$xml->xpath('Body' )
或仅使用以下语法访问子项:$xml->Body
。SimpleXML ignores the
env:
part of the XML structure and instead returns an object constructed like this:You can access the body with the following XPath query:
$xml->xpath('Body')
or just access the child by using the following syntax:$xml->Body
.