Link to home
Start Free TrialLog in
Avatar of j789
j789Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Read a .net dataset using php

I'm trying to use a web-service that has been written in C# .NET and returns a .NET dataset (see response below). The website is on a PHP platform. As you can see the information is there I just would like to know how to extract the values. Thanks.

Here's the response...

<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="LoginDet"><xs:element name="LoginDet" msdata:isdataset="true" msdata:maindatatable="Table" msdata:usecurrentlocale="true"><xs:complextype><xs:choice minoccurs="0" maxoccurs="unbounded"><xs:element name="Table"><xs:complextype><xs:sequence><xs:element name="LoginId" type="xs:int" minoccurs="0"><xs:element name="PasswordReminderTypeId" type="xs:int" minoccurs="0"><xs:element name="EmailId" type="xs:int" minoccurs="0"><xs:element name="LoginName" type="xs:string" minoccurs="0"><xs:element name="Password" type="xs:string" minoccurs="0"><xs:element name="MasterLoginId" type="xs:int" minoccurs="0"><xs:element name="NameId" type="xs:int" minoccurs="0"><xs:element name="UserEdit" type="xs:boolean" minoccurs="0"><xs:element name="Email" type="xs:string" minoccurs="0"><xs:element name="FullName" type="xs:string" minoccurs="0"><xs:element name="HasOwnership" type="xs:boolean" minoccurs="0"></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></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"><logindet xmlns=""><loginid>2223344432</loginid><passwordremindertypeid>342</passwordremindertypeid><emailid>313734</emailid><loginname>2222</loginname><password>11111</password><nameid>1079622</nameid><useredit>false</useredit><email>test@emailaddress.com</email><fullname>Mr Somebody</fullname><hasownership>true</hasownership><table diffgr:id="Table1" msdata:roworder="0"></table></logindet></diffgr:diffgram>

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Have you looked at SimpleXML?  http://www.w3schools.com/php/php_xml_simplexml.asp
Avatar of j789

ASKER

Hi Kaufmed, thanks for your response. I have looked at SimpleXML before but I get the following error when using simplexml_load_file...

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity
Do you have the XML stored in a file on your server? I got the impression you would have the data as a string in your code. If so, then I believe you should be using simplexml_load_string. My last post is a bit misleading in that regard. The next page, http://www.w3schools.com/php/php_ref_simplexml.asp , gives you a list of functions you can use with this library.
It looks as though you might have to strip out the inline schema in order to be able to use simpleXML with that sample.
Avatar of j789

ASKER

When using simplexml_load_string I get the following warning...

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Extra content at the end of the document

Can you please explain in more detail what you mean...

strip out the inline schema in order to be able to use simpleXML
The first part of your XML sample is the schema. If this is how you receive the XML from the web service, then you have an inline schema, which just means that the schema is embedded into the XML. In my testing, simpleXML did not function as expected when the schema was included. I used a simple preg_replace to strip it out.

Here is what I tested with:
<?php

	$content='<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="LoginDet"><xs:element name="LoginDet" msdata:isdataset="true" msdata:maindatatable="Table" msdata:usecurrentlocale="true"><xs:complextype><xs:choice minoccurs="0" maxoccurs="unbounded"><xs:element name="Table"><xs:complextype><xs:sequence><xs:element name="LoginId" type="xs:int" minoccurs="0"><xs:element name="PasswordReminderTypeId" type="xs:int" minoccurs="0"><xs:element name="EmailId" type="xs:int" minoccurs="0"><xs:element name="LoginName" type="xs:string" minoccurs="0"><xs:element name="Password" type="xs:string" minoccurs="0"><xs:element name="MasterLoginId" type="xs:int" minoccurs="0"><xs:element name="NameId" type="xs:int" minoccurs="0"><xs:element name="UserEdit" type="xs:boolean" minoccurs="0"><xs:element name="Email" type="xs:string" minoccurs="0"><xs:element name="FullName" type="xs:string" minoccurs="0"><xs:element name="HasOwnership" type="xs:boolean" minoccurs="0"></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></xs:element></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"><logindet xmlns=""><loginid>2223344432</loginid><passwordremindertypeid>342</passwordremindertypeid><emailid>313734</emailid><loginname>2222</loginname><password>11111</password><nameid>1079622</nameid><useredit>false</useredit><email>test@emailaddress.com</email><fullname>Mr Somebody</fullname><hasownership>true</hasownership><table diffgr:id="Table1" msdata:roworder="0"></table></logindet></diffgr:diffgram>';

	$xml = simplexml_load_string(preg_replace('#<(xs:schema).+?</\1>\s*#s', '', $content));

	var_dump($xml->xpath('/diffgr:diffgram/logindet/loginid'));
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of j789

ASKER

Thanks for your help Kaufmed.
Avatar of j789

ASKER

Quick, accurate response.
NP. Glad to help  = )