skip to Main Content

Here is the response in SOAP:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://sdsds.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <ns0:ServiceResponse xmlns:ns0="ababa.co.ke/Schemas/SAFService.xsd">
            <ns0:ResponseHeader>
                <ns0:ResponseCode>1</ns0:ResponseCode>
                <ns0:ResponseMsg>999</ns0:ResponseMsg>
            </ns0:ResponseHeader>
        </ns0:ServiceResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And now I am trying to get ResponseCode value as below

//not working

$obj = simplexml_load_string($xml);
$result = $obj->body->ServiceResponse->ResponseHeader->ResponseCode;
echo $result[0];
print_r($result);

//not working

$foo = new SimpleXMLElement($xml);
$bar = json_decode(json_encode($foo));
print_r($bar);

Can you please help or correct me where am I doing worng?

2

Answers


  1. Chosen as BEST ANSWER

    Finally I have done with SimpleXMLElement & got output exactly what I wished.

    $xml='<?xml version="1.0"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="sdsds/xmlsoap.org/soap/envelope/">
                <SOAP-ENV:Body>
                    <ns0:ServiceResponse xmlns:ns0="ababa.co.ke/Schemas/SAFService.xsd">
                <ns0:ResponseHeader>
                    <ns0:ResponseCode>1</ns0:ResponseCode>
                    <ns0:ResponseMsg>999</ns0:ResponseMsg>
                </ns0:ResponseHeader>
            </ns0:ServiceResponse>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>';
    
    $sxe=new SimpleXMLElement($xml);
    $sxe->registerXPathNamespace('x','ababa.co.ke/Schemas/SAFService.xsd');
    $result=$sxe->xpath('//x:ServiceResponse/x:ResponseHeader/x:ResponseCode');
    foreach ($result as $title)
    {
       echo "ResponseCode:".$title . "<br>";
    }
    

    Output will be

    ResponseCode:1
    

  2. A Quick example of using a namespace when querying the SOAP response – done using regular DOMDocument and DOMXPath but should be fairly straightforward with SimpleXML

    /*
        The uri used for the namespace does not necessarily as
        can be seen by the following nonsense uris.
    */
    $strxml='<SOAP-ENV:Envelope xmlns:SOAP-ENV="sdsds/xmlsoap.org/soap/envelope/">
                <SOAP-ENV:Body>
                    <ns0:ServiceResponse xmlns:ns0="ababa.co.ke/Schemas/SAFService.xsd">
                        <ns0:ResponseHeader>
                            <ns0:ResponseCode>1</ns0:ResponseCode>
                            <ns0:ResponseMsg>999</ns0:ResponseMsg>
                        </ns0:ResponseHeader>
                    </ns0:ServiceResponse>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>';
    
    libxml_use_internal_errors( true ) ;
    $dom=new DOMDocument();
    $dom->validateOnParse=false;
    $dom->strictErrorChecking=true;
    $dom->recover=true;
    $dom->loadXML( $strxml );
    $errors=libxml_get_errors();
    libxml_clear_errors();
    
    $xp=new DOMXPath( $dom );
    
    
    
    /* 
        Register the namespace - the prefix used here 
        does not necessarily need to match that used 
        in the SOAP response.
    */
    $xp->registerNameSpace('x','ababa.co.ke/Schemas/SAFService.xsd');
    $expr='//x:ServiceResponse/x:ResponseHeader/x:ResponseCode';
    
    // run the query
    $col=$xp->query( $expr );
    
    // process results, if any.
    if( $col && $col->length > 0 ){
        foreach( $col as $node )echo $node->localName,'->',$node->nodeValue;
    }else{
        echo 'XPath failed';
    }
    

    This will simply yield:

    ResponseCode->1
    

    update using SimpleXML

    $dom=new SimpleXMLElement( $strxml );
    $dom->registerXPathNamespace('x','ababa.co.ke/Schemas/SAFService.xsd');
    $expr='//x:ServiceResponse/x:ResponseHeader/x:ResponseCode';
    $col=$dom->xpath( $expr );
    if( !empty( $col ) ){
        foreach( $col as $node )echo 'result -> '.$node.br;
    }else{
        echo 'XPath failed';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search