mush
mush

Reputation: 165

xml is not valid - error 1002

I am struck by this error that I can't seem to resolve. I am getting an XML IS NOT VALID, error 1002.

I have checked certain xml validators online and they indicate xml is well formed. The xml is there to query an api but it doesn't seem to work.

Does anyone know what I'm doing wrong.

<SERVICE_SEARCH_REQUEST>
    <VERSION_HISTORY>APPLICATION_NAME=AppName XML_FILE_NAME =XMLFileName LICENCE_KEY=xxx-3939-xxxx-xxxx-xxx TS_API_VERSION = TSAPIVersion>
        <XML_VERSION_NO>3.0</XML_VERSION_NO>
    </VERSION_HISTORY>
    <GEO_LOCATION_NAME>London</GEO_LOCATION_NAME>
    <START_DATE>12 Jan 16</START_DATE>
    <NUMBER_OF_NIGHTS>1</NUMBER_OF_NIGHTS>
    <AVAILABLE_ONLY>TRUE</AVAILABLE_ONLY>
    <GET_START_PRICE>true</GET_START_PRICE>
    <ROOM_REPLY>
        <ANY_ROOM/>
    </ROOM_REPLY>
    <ROOMS_REQUIRED>
        <ROOM>
            <OCCUPANCY>2</OCCUPANCY>
            <QUANTITY>1</QUANTITY>
        </ROOM>
        <ROOM>
            <OCCUPANCY>3</OCCUPANCY>
            <QUANTITY>1</QUANTITY>
        </ROOM>
    </ROOMS_REQUIRED>
</SERVICE_SEARCH_REQUEST>

response i'm getting is:

Array ( [@attributes] => Array ( [CATEGORY] => XML Validation ) [VERSION_HISTORY] => Array ( [@attributes] => Array ( [APPLICATION_NAME] => AppName [XML_FILE_NAME] => XMLFileName [LICENCE_KEY] => LicenceKey [TS_API_VERSION] => TSAPIVersion ) [XML_VERSION_NO] => XMLVersionNumber ) [ERROR_NUMBER] => 1002 [ERROR_DESC] => XML is not valid ) 

updated php usage:

$header[] = "Accept: application/xml";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$XML);
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec($ch);
$response = simplexml_load_string($response);
//$desired_array = json_decode(json_encode((array);



print_r($response);
?>

updated error:

Warning: simplexml_load_string(): Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content on line 36
Warning: simplexml_load_string(): <?xml version="1.0" encoding="UTF-16"?><SERVICE_SEARCH_RESPONSE><VERSION_HISTORY on line 36
Warning: simplexml_load_string(): ^ on line 36

Upvotes: 0

Views: 798

Answers (1)

chris85
chris85

Reputation: 23880

This line:

<VERSION_HISTORY>APPLICATION_NAME=AppName XML_FILE_NAME =XMLFileName LICENCE_KEY=xxx-3939-xxxx-xxxx-xxx TS_API_VERSION = TSAPIVersion>

is not valid XML. The > is not closing an element. You probably want those values as attributes. Try:

<VERSION_HISTORY APPLICATION_NAME=\"AppName\" XML_FILE_NAME =\"XMLFileName\" LICENCE_KEY=\"xxx-3939-xxxx-xxxx-xxx\" TS_API_VERSION = \"TSAPIVersion\">

Per your update I've escaped the double quotes here because it sounds like you are using those for the string encapsulation in PHP. Alternatively you could use single quotes.

<VERSION_HISTORY APPLICATION_NAME='AppName' XML_FILE_NAME ='XMLFileName' LICENCE_KEY='xxx-3939-xxxx-xxxx-xxx' TS_API_VERSION = 'TSAPIVersion'>

In both cases PHP used such as

$xml = "<VERSION_HISTORY APPLICATION_NAME='AppName' XML_FILE_NAME ='XMLFileName' LICENCE_KEY='xxx-3939-xxxx-xxxx-xxx' TS_API_VERSION = 'TSAPIVersion'>";

or

$xml = "<VERSION_HISTORY APPLICATION_NAME=\"AppName\" XML_FILE_NAME =\"XMLFileName\" LICENCE_KEY=\"xxx-3939-xxxx-xxxx-xxx\" TS_API_VERSION = \"TSAPIVersion\">";

should be valid.

Upvotes: 1

Related Questions