【问题标题】:SimpleXML SOAP response Namespace issuesSimpleXML SOAP 响应命名空间问题
【发布时间】:2010-06-11 11:19:20
【问题描述】:

在为此花费了几个令人沮丧的时间之后,我正在寻求您的帮助。

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

回应是

<?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>

我正在尝试获取 的节点和子节点。 由于 XML 包含命名空间

$XmlArray   = new SimpleXMLElement($XmlStr);

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

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

还有关于这个网站的大约 20 个问题,很遗憾没有帮助。

即使写作,

$XmlArray   = new SimpleXMLElement($XmlStr);

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

给予

SimpleXMLElement Object
(
)

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

好像是行

$XmlArray   = new SimpleXMLElement($XmlStr);

没有按照我的预期去做。

非常欢迎任何有关如何从上述 XML 中获取节点的帮助。

显然,让它发挥作用(有一个有效的例子)是我在短期内需要的,但如果有人能帮助我理解我做错了什么,从长远来看会更好。

干杯。 斯图

【问题讨论】:

    标签: php xml namespaces simplexml


    【解决方案1】:

    您必须使用SimpleXMLElement::children(),尽管此时使用 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 中止

    【讨论】:

    • 谢谢!,我是如此接近。我已经研究了 children() 方法并正在使用这个 foreach ($XmlArray->children('env',true)->Envelope->children('env',true)->Body->children('ns1 ',true)->PlaceOrderResponse->ErrorArray->Error as $Error) 再次感谢 :-)在stackoverflow上正确格式化代码的标签是什么?我在旁边找不到。
    • @Stu 您只需按下 0101 按钮并选择您的代码。顺便说一句,如果答案确实令人满意,你应该接受它。
    【解决方案2】:

    一种廉价而讨厌的方法是简单地删除前缀:

    $xml = preg_replace('%<(\/)?(\S+):(\w+)%', '<$1$3', $xml);
    

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 2017-06-10
      相关资源
      最近更新 更多