Read xml obtained by nusoap in php

3

I am learning to perform webservice with SOAP and for this I used the library nusoap .

Everything is correct, the only problem is that I do not know how to deal with the xml once I get it, because it has a format like this:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
        <SOAP-ENV:Body>
            <ns1:Service.getViajesResponse xmlns:ns1="http://tempuri.org">
                <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="unnamed_struct_use_soapval[9]">
                    <item>
                        <title>Titulo</title>
                        <img>Imagen</img>
                    </item>
                    <item>
                        <title>Titulo</title>
                        <img>Imagen</img>
                    </item>

The code that I have in the client to read it is:

private function _soapResponse($result)
{
     $xmlResponse = $this->_soapClient->responseData;
     print_r($xmlResponse);
}

The print_r() shows the xml that I have indicated above.

How can I read it to save it in a array ???

    
asked by 15.11.2016 в 09:50
source

2 answers

2

I leave you a second option, if the Web Service returns an XML , you can convert it to JSON if it's easier to control the information.

For example:

  • Function to convert XML to JSON .
  • function xmlToJson(xml) 
    {
        var obj = {};
    
        if (xml.nodeType == 1) {
            if (xml.attributes.length > 0) {
            obj["@attributes"] = {};
                for (var j = 0; j < xml.attributes.length; j++) {
                    var attribute = xml.attributes.item(j);
                    obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
                }
            }
        } else if (xml.nodeType == 3) {
            obj = xml.nodeValue;
        }
        if (xml.hasChildNodes()) {
            for(var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                var nodeName = item.nodeName;
                if (typeof(obj[nodeName]) == "undefined") {
                    obj[nodeName] = xmlToJson(item);
                } else {
                    if (typeof(obj[nodeName].push) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                    }
                    obj[nodeName].push(xmlToJson(item));
                }
            }
        }
        return obj;
    };
    
  • Convert XML to JSON .
  • var json = xmlToJson(xml)
    
  • Conversion and access to JSON .
  • var json = xmlToJson(xml)
    var data = json.ArrayOfG_barras.g_barras;
    for (var i = 0; i < data.length; i++) {
    
        var id = parseInt(data[i].Valor["#text"]);
        var name = data[i].Nombre["#text"];
    }
    

    With this you can control the information in JSON format. As a clarification, the value json.ArrayOfG_barras.g_barras; I put it like this to access the node faster, because when converting the XML to JSON , I already brought that information, the answer of my web service ( ASMX ). You can verify your nodes from the console of any browser by printing in console the value of the variable JSON ( console.log(json); ).

        
    answered by 12.01.2017 в 01:49
    1

    Using simplexml_load_string and JSON you can do it, for example:

    private function _soapResponse($result)
    {
        $xmlResponse = $this->_soapClient->responseData;
        $array = json_decode(json_encode(simplexml_load_string($xmlResponse)),true);
        print_r($array);
    }
    

    Original answer here .

        
    answered by 15.11.2016 в 13:14