0

This is part of the XML SOAP response that I'm having trouble parsing:

<soap:Envelope><soap:Body><getTrainScheduleXMLResponse><getTrainScheduleXMLResult><STATION><STATION_2CHAR>NY</STATION_2CHAR><STATIONNAME>New York Penn Station</STATIONNAME><ITEMS><ITEM><ITEM_INDEX>0</ITEM_INDEX><SCHED_DEP_DATE>18:08:00 06/10/2013</SCHED_DEP_DATE><DESTINATION>MSU</DESTINATION><TRACK>6</TRACK><LINE>MNBTN</LINE><TRAIN_ID>6279</TRAIN_ID><STATUS>ALL ABOARD</STATUS><BACKCOLOR>brown</BACKCOLOR><FORECOLOR>white</FORECOLOR><SHADOWCOLOR>black</SHADOWCOLOR><GPSLATITUDE/><GPSLONGITUDE/><GPSTIME>6/10/2013 5:45:30 PM</GPSTIME><TRAIN_LINE>Montclair-Boonton Line</TRAIN_LINE><STATION_POSITION>0</STATION_POSITION><LINEABBREVIATION>MNBTN</LINEABBREVIATION><INLINEMSG/><STOPS><STOP><NAME>Newark Broad Street</NAME><TIME>6/10/2013 6:25:00 PM</TIME></STOP><STOP><NAME>Watsessing Avenue</NAME><TIME>6/10/2013 6:31:30 PM</TIME></STOP><STOP><NAME>Bloomfield</NAME><TIME>6/10/2013 6:34:00 PM</TIME></STOP><STOP><NAME>Glen Ridge</NAME><TIME>6/10/2013 6:36:30 PM</TIME></STOP><STOP><NAME>Bay Street</NAME><TIME>6/10/2013 6:39:30 PM</TIME></STOP><STOP><NAME>Walnut Street</NAME><TIME>6/10/2013 6:43:00 PM</TIME></STOP><STOP><NAME>Watchung Avenue</NAME><TIME>6/10/2013 6:45:30 PM</TIME></STOP><STOP><NAME>Upper Montclair</NAME><TIME>6/10/2013 6:48:30 PM</TIME></STOP><STOP><NAME>Mountain Avenue</NAME><TIME>6/10/2013 6:51:00 PM</TIME></STOP><STOP><NAME>Montclair Heights</NAME><TIME>6/10/2013 6:53:30 PM</TIME></STOP><STOP><NAME>Montclair State U</NAME><TIME>6/10/2013 6:59:00 PM</TIME></STOP></STOPS></ITEM>

I have tried doing this:

$xmlstr = file_get_contents("data.xml");
$xml = new SimpleXMLElement($xmlstr);
var_dump($xml);

But it returns output of this:

object(SimpleXMLElement)#1 (0) {
}

Which I take to mean it's empty? How can I parse this XML in PHP? This isn't a complete file provided. Thanks!

Edward
  • 9,430
  • 19
  • 48
  • 71
  • Have you read about the PHP [SoapClient](http://php.net/manual/en/class.soapclient.php) class? How are you receiving this request in the first place? You shouldn't have to attempt to consume a SOAP response manually, but its the name space that's probably tricking things up. http://stackoverflow.com/questions/4194489/how-to-parse-soap-xml – Scuzzy Jun 10 '13 at 22:18
  • Take a look at a basic usage of SimpleXMLElement :) http://www.php.net/manual/en/simplexml.examples-basic.php – Dovydas Navickas Jun 10 '13 at 22:20
  • 1
    @Scuzzy I am doing a SOAP request that is requesting XML. I saved the output to a file so I could work on parsing it without hammer the server. – Edward Jun 10 '13 at 22:22
  • I've run your code locally and I get the error "Namespace prefix soap on Envelope is not defined" which is indicative of no xmlns attribute. I could suggest is you setup a mock version of the SOAP server, connect to that and have it simply echo's your canned response (might need to set http headers mind you). – Scuzzy Jun 10 '13 at 22:28
  • Oh, your sample XML is truncated? Document stops after – Scuzzy Jun 10 '13 at 22:30
  • Yes, I didn't include the whole thing. – Edward Jun 10 '13 at 22:31
  • How can the be parsed through this as XML? Or does it require a special treatment? I tried the SoapClient but was unable to figure out how to send the SOAP request and ended up using CURL. – Edward Jun 10 '13 at 22:32
  • @Edward If the remote web service you indent to call has WSDL document, you should have no problems using the build in soap client, Otherwise, you're going to have to start stripping out soap namespaces to pass it through simplexml str_replace(' – Scuzzy Jun 10 '13 at 22:42
  • I see, so I need to master the SoapClient to solve this? – Edward Jun 10 '13 at 22:44
  • 1
    @Edward yes, it makes life so much easier because you simply do not have to manage the transmission of the request at all, its all done by the soap client class. – Scuzzy Jun 10 '13 at 22:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31554/discussion-between-scuzzy-and-edward) – Scuzzy Jun 10 '13 at 23:12

1 Answers1

1

This is very much so a hack, because ideal you should consume Soap requests with the SoapClient class, but if you were to strip out the soap namespace, you should be able to feed your request through simpleXML

$xml = new SimpleXMLElement(str_replace('<soap:','<',$xmlstr));

To consume a soap service if you've been given a WSDL might be as simple as...

$client = new SoapClient('http://www.domain.com/service/soap.wsdl');
$result = $client->someMethodCall($params,$params);
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • How do I sent a SOAP request? I have the XML formatted for it, but ended up using CURL which I don't want to do. Is there a way to send this request along with the new SoapClient function to the URL? – Edward Jun 10 '13 at 22:56
  • Ok so don't so think of Soap as dealing with the raw XML, you feed in parameters or objects into the SoapClient depending on the requirements of the web service API, and it in turn returns a objectified response. Lets move this off the question page http://chat.stackoverflow.com/rooms/31554/discussion-between-scuzzy-and-edward – Scuzzy Jun 10 '13 at 23:11