xml无效 - 错误1002

时间:2016-01-03 22:00:41

标签: php xml

我对这个似乎无法解决的错误感到震惊。我得到的XML不是有效的,错误1002。

我已经在线检查了某些xml验证器,它们表明xml格式正确。 xml用于查询api,但它似乎不起作用。

有谁知道我做错了什么。

<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>

我得到的回应是:

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 ) 

更新了php使用情况:

$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);
?>

更新错误:

  

警告:simplexml_load_string():实体:第1行:解析器错误:文档标记为UTF-16但在第36行上有UTF-8内容
  警告:第36行的simplexml_load_string():<?xml version="1.0" encoding="UTF-16"?><SERVICE_SEARCH_RESPONSE><VERSION_HISTORY
  警告:第36行的simplexml_load_string():^

1 个答案:

答案 0 :(得分:1)

这一行:

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

不是有效的XML。 >未关闭元素。您可能希望将这些值作为属性。尝试:

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

根据您的更新,我在这里转义了双引号,因为听起来您正在使用PHP中的字符串封装。或者,您可以使用单引号。

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

在两种情况下都使用了PHP,例如

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

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

应该有效。