SimpleXML 解析问题

发布于 2024-11-16 21:05:46 字数 898 浏览 0 评论 0原文

所有,

尝试解析此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2024-11-23 21:05:46

尝试将命名空间注册到 'env'

$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');

编辑
此代码设置 body 中子元素的命名空间。

$bodies = $xml->xpath('env:Body');
foreach($bodies as $body){
    $reply = $body->children('v4', TRUE)->TrackReply;
    var_dump($reply);
}

或者,您可以直接使用以下命令获取类似 TrackReply 的元素:

$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('v4', 'http://fedex.com/ws/track/v4');
var_dump($xml->xpath('env:Body/v4: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

$bodies = $xml->xpath('env:Body');
foreach($bodies as $body){
    $reply = $body->children('v4', TRUE)->TrackReply;
    var_dump($reply);
}

Alternatively, you can get an element like TrackReply directly with:

$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('v4', 'http://fedex.com/ws/track/v4');
var_dump($xml->xpath('env:Body/v4:TrackReply'));
温馨耳语 2024-11-23 21:05:46

SimpleXML 忽略 XML 结构的 env: 部分,而是返回如下构造的对象:

SimpleXMLElement Object
(
    [Header] => SimpleXMLElement Object
        (
        )

    [Body] => SimpleXMLElement Object
        (
        )

)

您可以使用以下 XPath 查询访问正文: $xml->xpath('Body' ) 或仅使用以下语法访问子项:$xml->Body

SimpleXML ignores the env: part of the XML structure and instead returns an object constructed like this:

SimpleXMLElement Object
(
    [Header] => SimpleXMLElement Object
        (
        )

    [Body] => SimpleXMLElement Object
        (
        )

)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文