SimpleXML SOAP 响应命名空间问题

2024-05-11php开发问题
3

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

问题描述

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

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

回应是

<?xml version="1.0" encoding="UTF-8"?><env:信封 xmlns:env="<a href="http://www.w3.org/2003/05/soap-envelope">http://www.w3.org/2003/05/肥皂信封</a>"<xmlns:ns1="<a href="http://soap.xxxxxx.co.uk/">http://soap.xxxxxx.co.uk/</a>"><env:身体><ns1:PlaceOrderResponse><xxxxxOrderNumber></xxxxxOrderNumber><错误数组><错误><错误代码>24</错误代码><ErrorText>+client+order+number+3002254+is+already+in+use</ErrorText></错误><错误><错误代码>1</错误代码><ErrorText>正在中止</ErrorText></错误></错误数组></ns1:PlaceOrderResponse></env:正文></env:信封>

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

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

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

  • 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 个问题,很遗憾没有帮助.

即使写作,

$XmlArray = new SimpleXMLElement($XmlStr);echo "<br/><br/><pre>
";print_r($XmlArray);echo "<pre><br/><br/>
";

给予

SimpleXMLElement 对象()

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

好像是行

$XmlArray = new SimpleXMLElement($XmlStr);

没有按照我的预期去做.

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

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

干杯.斯图

解决方案

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

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+use1 中止

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

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

The response is

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

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/

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

Even writing,

$XmlArray   = new SimpleXMLElement($XmlStr);

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

gives

SimpleXMLElement Object
(
)

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

It seems that the line

$XmlArray   = new SimpleXMLElement($XmlStr);

is not doing what I expect it to.

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.

Cheers. Stu

解决方案

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 />";
    } 

gives:

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

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17