You are here:  » Parsing Header Information Within SOAP Enveloper


Parsing Header Information Within SOAP Enveloper

Submitted by neilhook on Fri, 2007-06-29 22:30 in

Hi Guys,

I've been taking a look at Magic Parser and am wondering whether there's anyway of performing this SOAP request:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>
<m:WSAuthenticate xmlns:m="http://beta-DataTimeTableToolKit.com/">
<m:CustomerRefCode>fltdir</m:CustomerRefCode>
<m:Password>password123</m:Password>
<m:WebServicesRefCode>tks</m:WebServicesRefCode>
<m:SrcIPAddress/>
</m:WSAuthenticate>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<m:GetStationList xmlns:m="http://beta-DataTimeTableToolKit.com/">
<m:_sStationSearchXML>&lt;?xml version="1.0"?&gt;&lt;stationListSearch customerCode="fltdir" productCode="External" code="LON" codeType="CTY" direction="IN" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="GetStationList_Input.xsd"/&gt;</m:_sStationSearchXML>
</m:GetStationList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I've been rattling my brains using other PHP methods and I hope Magic Parser will be the answer to all prayers.

Take Care

Submitted by support on Sat, 2007-06-30 05:45

Hello Neil,

Magic Parser is not a SOAP library as such, and therefore there is no way (using this script at least) to submit the above SOAP request. You would need to do this using PHPs usual options for using SOAP (e.g. the library built into PHP5, or NuSOAP for example), and then perhaps - if the XML is suitable - use Magic Parser to parse data from the response.

Once you have obtained the response XML via your SOAP library, you can save a copy to your local computer and then upload it to the demo tool to see if it is easily compatible with Magic Parser...

Cheers,
David.

Submitted by neilhook on Sat, 2007-06-30 11:43

Hi David,

That's fine, as I can change the Header using NuSOAP setHeaders command.. I believe (first time I've had to do this).

From what I've seen and experienced of Magic Parser, it seems to make life alot easier. My question is, using the following xml reply as an example, how should I pass it to Magic Parser and then walk through the records? For our purposes we're interested in the stationlist, but I fully understand that other fellow coders may be interested in the whole thing.

<soap:Body>
  <GetStationListResponse xmlns="http://beta-DataTimeTableToolKit.com/">
    <GetStationListResult>
      <stationList>
        <stations count="5">
          <station code="LCY" name="London City Airport" cityCode="LON" metroCityCode="LON" />
          <station code="LGW" name="Gatwick Airport" cityCode="LON" metroCityCode="LON" />
          <station code="LHR" name="Heathrow Airport" cityCode="LON" metroCityCode="LON" />
          <station code="LTN" name="Luton Airport" cityCode="LON" metroCityCode="LON" />
          <station code="STN" name="Stansted Airport" cityCode="LON" metroCityCode="LON" />
        </stations>
        <cities count="1">
          <city code="LON" name="London" countryCode="GB" lat="51.30972222" lon="-0.32472222"/>
        </cities>
        <metros count="1">
          <metro code="LON" name="London"/>
        </metros>
        <countries count="1">
          <country code="GB" name="United Kingdom" regioncode="EUR"/>
        </countries>
        <regions count="1">
          <region code="EUR" name="Europe"/>
        </regions>
      </stationList>
    </GetStationListResult>
  </GetStationListResponse>
</soap:Body>

Once again many thanks for your professional help.

Neil.

Submitted by support on Sat, 2007-06-30 12:18

Hi Neil,

I've written a quick example based on your response XML from your post above. I've included this inline in the following code as a string in the variable $xml - in your case this would of course be the response from your call to the SOAP library. Note how the script uses "string://".$xml as the filename parameter to MagicParser_parse() - this is how you parse a string rather than a file - simply change $xml to the name of the variable in which your SOAP XML response document is in.

Here's the output running on this server:
http://www.magicparser.com/examples/stationlist.php

Here's the source:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<p>".$record["STATION-NAME"]."</p>";
  }
$xml =
'<soap:Body>
  <GetStationListResponse xmlns="http://beta-DataTimeTableToolKit.com/">
    <GetStationListResult>
      <stationList>
        <stations count="5">
          <station code="LCY" name="London City Airport" cityCode="LON" metroCityCode="LON" />
          <station code="LGW" name="Gatwick Airport" cityCode="LON" metroCityCode="LON" />
          <station code="LHR" name="Heathrow Airport" cityCode="LON" metroCityCode="LON" />
          <station code="LTN" name="Luton Airport" cityCode="LON" metroCityCode="LON" />
          <station code="STN" name="Stansted Airport" cityCode="LON" metroCityCode="LON" />
        </stations>
        <cities count="1">
          <city code="LON" name="London" countryCode="GB" lat="51.30972222" lon="-0.32472222"/>
        </cities>
        <metros count="1">
          <metro code="LON" name="London"/>
        </metros>
        <countries count="1">
          <country code="GB" name="United Kingdom" regioncode="EUR"/>
        </countries>
        <regions count="1">
          <region code="EUR" name="Europe"/>
        </regions>
      </stationList>
    </GetStationListResult>
  </GetStationListResponse>
</soap:Body>'
;
  
$formatString "xml|SOAP:BODY/GETSTATIONLISTRESPONSE/GETSTATIONLISTRESULT/STATIONLIST/STATIONS/STATION/";
  
MagicParser_parse("string://".$xml,"myRecordHandler",$formatString);
?>

Points to note:

$formatString describes the repeating information that you want Magic Parser to extract from the XML. In this case, myRecordHandler() will be called once for each STATION. Now, because the information in this particular XML is all in attributes, the key you need into the $record array for the name attribute is "STATION-NAME", which you can see in the above example.

Hope this helps!
Cheers,
David.