SimpleXML SOAP响应命名空间问题 [英] SimpleXML SOAP response Namespace issues

查看:76
本文介绍了SimpleXML SOAP响应命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此上花了几个令人沮丧的时间后,我正在寻求您的帮助.

After spending SEVERAL frustrated hours on this I am asking for your help.

我正在尝试从SOAP响应中获取特定节点的内容.

I am trying to get the content of particular nodes from a SOAP response.

响应是

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="<a href="http://www.w3.org/2003/05/soap-envelope">http://www.w3.org/2003/05/soap-envelope</a>"<xmlns:ns1="<a href="http://soap.xxxxxx.co.uk/">http://soap.xxxxxx.co.uk/</a>">
    <env:Body>
        <ns1:PlaceOrderResponse>
            <xxxxxOrderNumber></xxxxxOrderNumber>
            <ErrorArray>
                <Error>
                    <ErrorCode>24</ErrorCode>
                    <ErrorText>The+client+order+number+3002254+is+already+in+use</ErrorText>
                </Error>
                <Error>
                    <ErrorCode>1</ErrorCode>
                    <ErrorText>Aborting</ErrorText>
                </Error>
            </ErrorArray>
        </ns1:PlaceOrderResponse>
    </env:Body>
</env:Envelope>

我试图到达< ErrorArray>的节点和子节点. 由于XML包含名称空间

I am trying to get at the nodes and children of <ErrorArray>. Because of the XML containing namespaces

$XmlArray   = new SimpleXMLElement($XmlStr);

foreach ($XmlArray->env:Envelope->env:Body->ns1:PlaceOrderResponse->ErrorArray->Error as $Error)
{
    echo $Error->ErrorCode."<br />";
}

不起作用. 我已经阅读了许多文章,例如

doesn't work. I have read a number of articles such as

  • http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/
  • http://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/

以及该网站上的大约20个问题,很遗憾,这些问题没有帮助.

and about 20 questions on this site, which unfortunately are not helping.

即使是写作,

$XmlArray   = new SimpleXMLElement($XmlStr);

echo "<br /><br /><pre>\n";
print_r($XmlArray);
echo "<pre><br /><br />\n";

给予

SimpleXMLElement Object
(
)

这让我想知道肥皂响应($ XmlStr)是否实际上是SimpleXMLElement的有效输入.

which makes me wonder if the soap response ($XmlStr) is actually a valid input for SimpleXMLElement.

似乎是线

$XmlArray   = new SimpleXMLElement($XmlStr);

没有按照我的期望去做.

is not doing what I expect it to.

非常欢迎您提供有关如何从上述XML中获取节点的帮助.

Any help on how to get the nodes from the XML above would be very welcome.

从短期来看,显然需要它(有一个可行的示例),但是从长远来看,如果有人可以帮助我了解我在做错的事情会更好.

Obviously getting it to work (having a working example) is what I need in the short term, but if someone could help me understand what I am doing wrong would be better in the long term.

干杯. 斯图

推荐答案

您必须使用 SimpleXMLElement::children() ,尽管此时使用XPath可能会更容易.

You have to use SimpleXMLElement::children(), though at this point it would probably be easier to use XPath.

<?php
    $XmlStr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"  xmlns:ns1="http://soap.xxxxxx.co.uk/" >
    <env:Body>
        <ns1:PlaceOrderResponse>
            <xxxxxOrderNumber></xxxxxOrderNumber>
            <ErrorArray>
                <Error>
                    <ErrorCode>24</ErrorCode>
                    <ErrorText>The+client+order+number+3002254+is+already+in+use</ErrorText>
                </Error>
                <Error>
                    <ErrorCode>1</ErrorCode>
                    <ErrorText>Aborting</ErrorText>
                </Error>
            </ErrorArray>
        </ns1:PlaceOrderResponse>
    </env:Body>
</env:Envelope>
XML;

    $XmlArray   = new SimpleXMLElement($XmlStr);

    $t = $XmlArray->children("env", true)->Body->
        children("ns1", true)->PlaceOrderResponse->
        children()->ErrorArray->Error;
    foreach ($t as $error) {
        echo $error->ErrorCode, " " , $error->ErrorText, "<br />";
    } 

给予:


24 The+client+order+number+3002254+is+already+in+use
1 Aborting

这篇关于SimpleXML SOAP响应命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆