Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing SOAP response

Calling a web service from my controller:

$client = new \SoapClient("http://.../webservice/NAME_OF_PAGE.asmx?WSDL");
$result = $client->EstadoHabitacionesFechas();

I get this:

<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="TablaEstadoHabitacion" msdata:UseCurrentLocale="true">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="TablaEstadoHabitacion">
                    <xs:complexType><xs:sequence>
                        <xs:element name="IdHabitacion" type="xs:int" minOccurs="0"/>
                        <xs:element name="FechaEntrada" type="xs:string" minOccurs="0"/>
                        <xs:element name="FechaSalida" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:choice>
    </xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <DocumentElement xmlns="">
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
            <IdHabitacion>1</IdHabitacion>
            <FechaEntrada>23/05/2012</FechaEntrada>
            <FechaSalida>31/12/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
            <IdHabitacion>2</IdHabitacion>
            <FechaEntrada>23/05/2012</FechaEntrada>
            <FechaSalida>29/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
            <IdHabitacion>2</IdHabitacion>
            <FechaEntrada>29/06/2012</FechaEntrada>
            <FechaSalida>01/07/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
            <IdHabitacion>3</IdHabitacion>
            <FechaEntrada>02/06/2012</FechaEntrada>
            <FechaSalida>03/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion5" msdata:rowOrder="4" diffgr:hasChanges="inserted">
            <IdHabitacion>3</IdHabitacion>
            <FechaEntrada>29/06/2012</FechaEntrada>
            <FechaSalida>01/07/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion6" msdata:rowOrder="5" diffgr:hasChanges="inserted">
            <IdHabitacion>4</IdHabitacion>
            <FechaEntrada>29/06/2012</FechaEntrada>
            <FechaSalida>01/07/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion7" msdata:rowOrder="6" diffgr:hasChanges="inserted">
            <IdHabitacion>5</IdHabitacion>
            <FechaEntrada>02/06/2012</FechaEntrada>
            <FechaSalida>03/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
        <TablaEstadoHabitacion diffgr:id="TablaEstadoHabitacion20" msdata:rowOrder="19" diffgr:hasChanges="inserted">
            <IdHabitacion>10</IdHabitacion>
            <FechaEntrada>02/06/2012</FechaEntrada>
            <FechaSalida>03/06/2012</FechaSalida>
        </TablaEstadoHabitacion>
    </DocumentElement>
</diffgr:diffgram>

How can I parse this data and use it?

like image 225
Kioko Kiaza Avatar asked Jul 03 '12 16:07

Kioko Kiaza


People also ask

How do I parse SOAP response in Servicenow?

You can do first that response data in stringformat if it is getting then you can parse it. JSON. stringify(); there is method you can use that method. then write your script.

Does SOAP work with XML?

SOAP works with XML only The two most popular data formats are XML and JSON. XML (or Extensible Markup Language) is a text format that establishes a set of rules to structure messages as both human- and machine-readable records. But XML is verbose as it aims at creating a web document with all its formality.


1 Answers

You don't make very clear what "use" is, but you clearly need some form of XML parsing/search.

For example, try xml-loading that string and var_dump the result. Simply enumerating the various properties should show you the opportunities.

Later on, you might try XPath search and more advanced "tricks" to speed up the work.

    // Remove namespaces
    $xml    = str_replace(array("diffgr:","msdata:"),'', $xml);
    // Wrap into root element to make it standard XML
    $xml    = "<package>".$xml."</package>";
    // Parse with SimpleXML - probably there're much better ways
    $data   = simplexml_load_string($xml);
    $rooms  = $data->package->diffgram->DocumentElement->TablaEstadoHabitacion;
    print "We have " . count($rooms) . " rooms: \n";
    foreach($rooms as $i => $room)
    {
            print "Room {$i}: id={$room['id']} (official id: {$room->IdHabitacion}\n";
            print "Entrada {$room->FechaEntrada}, salida {$room->FechaSalida}\n...\n";
    }

There are several parsers you can use, this is a quick and dirty one.

See more here.

Large data sets

Note: for very large XML data sets, I've found out that foreach is best.

And for large data sets where you only need a few information, and the whole file might not fit into available memory, you will probably want to use XMLParser, or XMLReader, and sift the whole file through the parser while keeping/manipulating (e.g. sending in a DB, or displaying to HTML) only the information you need.

While this isn't in general good practice, you can turn output buffering off before entering a long XML parsing loop, outputting HTML as soon as you have it and flush()ing once in a while. This will outsource the HTML to the HTTP server, taking up less memory in the PHP process, at the expense of slightly inferior compression (if you output chunks of HTML of more than about 40K, the difference is negligible) and proportionally better responsivity (the user "sees" something happen faster, even if overall operation completion takes a little longer. The experience is that of a faster load).

like image 129
LSerni Avatar answered Nov 03 '22 02:11

LSerni