Viewed   77 times

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>n";
print_r($XmlArray);
echo "<pre><br /><br />n";

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

 Answers

2

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
Friday, August 26, 2022
3

Are you using SimpleXML?

A solution for you seems to exist right here.

EDIT

Answer copied here for posterity's sake

Access the children by their XML namespace.

$dcChildren = $node->children( 'http://purl.org/dc/elements/1.1/' );

$title = $dcChildren->title;
Thursday, August 18, 2022
 
o.rares
 
2

SimpleXML creates a tree object, so you have to follow that tree to get to the nodes you want.

Also, you have to use the actual namespace URI when accessing it, e.g.: urn:eCaseWSDL instead of ns1:

Try this:

$soap = $xml_element->children($name_spaces['SOAP-ENV'])
                    ->Body
                    ->children($name_spaces['ns1'])
                    ->registerDocumentByPatientResponse
                    ->children();

var_dump((string)$soap->returnArray->id); // 138
Friday, August 5, 2022
3

Usually, people use children().

$rss = simplexml_load_string(
    '<?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:moshtix="http://www.moshtix.com.au">
        <channel>
            <link>qweqwe</link>
            <moshtix:genre>asdasd</moshtix:genre>
        </channel>
    </rss>'
);

foreach ($rss->channel as $channel)
{
    echo 'link: ', $channel->link, "n";
    echo 'genre: ', $channel->children('moshtix', true)->genre, "n";
}
Friday, August 12, 2022
 
3

If you do a lot of work with this, I would definitively recommend using JAXB as MGoron suggests. If this is a one shot excersize, XPATH could also work well.

    /*
     * Must use a namespace aware factory
     */
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(...);


    /*
     * Create an XPath object
     */
    XPath p = XPathFactory.newInstance().newXPath();

    /*
     * Must use a namespace context
     */
    p.setNamespaceContext(new NamespaceContext() {

        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

        public String getPrefix(String namespaceURI) {
            return null;
        }

        public String getNamespaceURI(String prefix) {
            if (prefix.equals("ns1"))
                return "http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema";
            if (prefix.equals("cm"))
                return "http://www.ibm.com/xmlns/db2/cm/api/1.0/schema";
            return null;
        }
    });

    /*
     * Find the ICCSFileName attribute
     */
    Node iccsFileName = (Node) p.evaluate("//ns1:ICCSPArchivSuche/@ICCFileName", doc, XPathConstants.NODE);
    System.out.println(iccsFileName.getNodeValue());

    /*
     * Find the URL
     */
    Node url = (Node) p.evaluate("//ns1:ICCSPArchivSuche/cm:resourceObject/cm:URL/@value", doc, XPathConstants.NODE);
    System.out.println(url.getNodeValue());
Friday, November 4, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :