在为此花费了几个令人沮丧的小时后,我正在寻求您的帮助.
我正在尝试从 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/>";}
不起作用.我已经阅读了许多文章,例如
还有关于这个网站的大约 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
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 响应命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!