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?
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_dumpthe 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.
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
foreachis 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).