Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

php - SimpleXML SOAP response Namespace issues

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...