0

I parsed HTML content as a SOAP response but I can't retrieve it in client.

This is the PHP code I used to parse the SOAP response:

$obj = simplexml_load_string(read_file('../soap.xml'));
var_dump($obj->children('http://schemas.xmlsoap.org/soap/envelope/'));

Here is the SOAP response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetDetailsResponse xmlns="http://192.168.2.34:180/">
      <GetDetailsResult>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
        &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      </GetDetailsResult>
    </GetDetailsResponse>
  </soap:Body>
</soap:Envelope>

It shows the following error:

A PHP Error was encountered

Severity: Warning

Message: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document

Filename: controller.php

Line Number: 60

A PHP Error was encountered

Severity: Warning

Message: simplexml_load_string():

A PHP Error was encountered

Severity: Warning

Message: simplexml_load_string(): ^

Filename: controller.php

Line Number: 60

Please help me parse it using PHP?

jasonlam604
  • 1,456
  • 2
  • 16
  • 25
linto cheeran
  • 1,440
  • 1
  • 17
  • 30

2 Answers2

0

What is the output of var_dump($obj->children('http://schemas.xmlsoap.org/soap/envelope/'));

dkasipovic
  • 5,930
  • 1
  • 19
  • 25
  • A PHP Error was encountered Severity: Warning Message: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document Filename: controllers/pte.php Line Number: 60 A PHP Error was encountered Severity: Warning Message: simplexml_load_string(): A PHP Error was encountered Severity: Warning Message: simplexml_load_string(): ^ Filename: controllers/pte.php Line Number: 60 – linto cheeran Feb 24 '14 at 18:02
  • I suggest two things: 1. $xml = 'YOUR RESPONSE HERE'; $obj = simplexml_load_string(read_file($xml)); var_dump($obj->children('http://schemas.xmlsoap.org/soap/envelope/')); and 2: var_dump(readfile('../soap.xml')); It is possible that there are some extra characters at the beginning of your xml file. – dkasipovic Feb 24 '14 at 18:17
0

I have a Simple Solution for that How To Parse SOAP Response

i have xml resopnse file is

<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <xyzResponse xmlns="http://tempuri.org/">
                <xyzResult>
                    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="Table">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="myCityID" type="xs:int" minOccurs="0" />
                                                <xs:element name="myCityName" type="xs:string" minOccurs="0" />

                                                <xs:element name="myLat" type="xs:double" minOccurs="0" />
                                                <xs:element name="myLon" type="xs:double" 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">
                        <NewDataSet xmlns="">
                            <Table diffgr:id="Table1" msdata:rowOrder="0">
                                <CityID>1</CityID>
                                <CityName>Ahmedabad</CityName>
                                <Lat>23.045839</Lat>
                                <Lon>72.550578</Lon>
                            </Table>
                            <Table diffgr:id="Table2" msdata:rowOrder="1">
                                <CityID>21</CityID>
                                <CityName>Amritsar</CityName>
                                <Lat>31.705603</Lat>
                                <Lon>74.807337</Lon>
                            </Table>
                        </NewDataSet>
                    </diffgr:diffgram>
                </xyzResult>
            </xyzResponse>
        </soap:Body>
    </soap:Envelope>

i want to retrieve only tables data then following code is use

            $sxe = new SimpleXMLElement($response);//in $response varible its a xml file or xml response
            $sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
            $result = $sxe->xpath("//NewDataSet");
            echo "<pre>";
            //print_r($result[0]);
            foreach ($result[0] as $title) {
                print_r($title);
                //echo $title->CityID;
            }

For more information visit

https://vaja906programmingworld.wordpress.com/

Post Name : How To Parse SOAPResponse Using PHP

Hina Vaja
  • 314
  • 1
  • 5
  • 15