php XML 解析错误 - 文档末尾的额外内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16972737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Parse Error - Extra content at the end of the document
提问by JD Vangsness
I am trying to give the option to get either a JSON or XML response.
我正在尝试提供获取 JSON 或 XML 响应的选项。
JSON works fine but when I view the XML, I get an error.
JSON 工作正常,但当我查看 XML 时,出现错误。
http://lmsapi.com/?api_key=fba9e59d7af86b239e82581780ff987e&format=jsonhttp://lmsapi.com/?api_key=fba9e59d7af86b239e82581780ff987e&format=xml
http://lmsapi.com/?api_key=fba9e59d7af86b239e82581780ff987e&format=json http://lmsapi.com/?api_key=fba9e59d7af86b239e82581780ff987e&format=xml
I'm not sure what is going wrong. I have searched and the only thing I see is to make sure that I don't have any extra whitespace or even have spaces in my element names, which I don't.
我不确定出了什么问题。我已经搜索过,我看到的唯一一件事是确保我的元素名称中没有任何多余的空格,甚至没有空格,而我没有。
index.php
索引.php
<?php
require_once('includes/inc.settings.php');
if(${'Response'}['format'] == 'json'){
print_r(${'Results'});
}elseif(${'Response'}['format'] == 'xml'){
header('Content-type: application/xml');
echo "<?xml version=\"1.0\"?>";
echo ${'Results'};
}
?>
inc.settings.php
inc.settings.php
<?php
// ****************************************
// Require other includes
// ****************************************
require('Excel/PHPExcel.php');
require('inc.variables.php');
require('inc.functions.php');
require('inc.browser.php');
require('class.phpmailer.php');
// ****************************************
// Initial page setup
// ****************************************
// Set our Error Handling
if($debug == true){
error_reporting(E_ALL);
ini_set('display_errors', 1);
} else {
error_reporting(E_ERROR);
ini_set('display_errors', 1);
}
// Set our timeout limit
set_time_limit(30);
// Set our memory limit
ini_set('memory_limit', '128M');
// Start our PHP session
session_start();
// Set our charset to UTF-8
mb_internal_encoding('UTF-8');
// Get our browser information
$browser = new Browser;
// ****************************************
// Connect to mySQL
// ****************************************
mysql_connect(MYSQL_HOST.':'.MYSQL_PORT, MYSQL_USER, MYSQL_PASS) or die('Could not establish a connection to the MySQL Engine.');
mysql_select_db(MYSQL_DB) or die('Could not locate the specified database');
// ****************************************
// Sanitize our possible api data
// ****************************************
if(isset($_GET['api_key'])) { ${'API KEY'} = inputCleanSQL($_GET['api_key']); } else { ${'API KEY'} = ''; }
if(isset($_GET['format'])){ ${'Format'} = inputCleanSQL($_GET['format']); } else { ${'Format'} = ''; }
if(isset($_GET['act'])){ ${'Action'} = inputCleanSQL($_GET['act']); } else { ${'Action'} = ''; }
if(isset($_GET['phone_numer'])){ ${'Phone Number'} = inputCleanSQL(removeCHARSphone($_GET['phone_number'])); } else { ${'Phone Number'} = ''; }
if(isset($_GET['limit'])){ ${'Limit'} = inputCleanSQL($_GET['limit']); } else { ${'Limit'} = ''; }
// ****************************************
// Begin the Response Array
// ****************************************
${'Response'} = array();
${'Response'}['status'] = '';
${'Response'}['reason'] = array();
${'Format Type'} = true;
// Check the API Key
if(isset(${'API KEY'})){
${'API Key Check'} = mysql_result(mysql_query('SELECT count(0) FROM `api`.`api_keys` WHERE `api_key` = "'.${'API KEY'}.'"'),0);
if(!${'API Key Check'}) {
${'Response'}['status'] = 'failed';
${'Response'}['reason'][] = 'invalid api key';
} else {
// Log the API hit
mysql_query('INSERT INTO `api`.`api_log` (`api_key`) VALUES ("'.${'API KEY'}.'")');
// Check the format request
if(${'Format'} != '' && ${'Format'} != 'json' && ${'Format'} != 'xml'){
${'Response'}['status'] = 'failed';
${'Response'}['reason'][] = 'invalid format specified (&format=)';
${'Format Type'} = false;
}
// Check the action request
if(${'Action'} != '' && ${'Action'} != 'get' && ${'Action'} != 'details'){
${'Response'}['status'] = 'failed';
${'Response'}['reason'][] = 'invalid action type (&act=)';
}
if(${'Response'}['status'] != 'failed'){
${'Response'}['status'] = 'success';
unset(${'Response'}['reason']);
}
}
} else {
${'Response'}['status'] = 'failed';
${'Response'}['reason'][] = 'api key required';
}
if(isset(${'Format'}) && ${'Format Type'}){
if(${'Format'} == 'json'){
${'Response'}['format'] = 'json';
${'Results'} = json_encode(${'Response'});
} elseif(${'Format'} == 'xml'){
${'Response'}['format'] = 'xml';
${'Results'} = arrayToXML(${'Response'});
}
} else {
${'Response'}['format'] = 'json';
${'Results'} = json_encode(${'Response'});
}
?>
Any help would be much appreciated!
任何帮助将非常感激!
回答by salathe
XML can only have one "document entity" or "root", you're trying to use two (status
and format
). Wrap your two elements in a single one, so that your XML document only has one root element.
XML 只能有一个“文档实体”或“根”,您正在尝试使用两个(status
和format
)。将两个元素合二为一,这样您的 XML 文档就只有一个根元素。
Bad
坏的
<?xml version="1.0"?>
<status>success</status>
<format>xml</format>
Good
好的
<?xml version="1.0"?>
<response>
<status>success</status>
<format>xml</format>
</response>
回答by zmonteca
Make sure you also triple check for a duplicate urlset declaration at the top of your document. This was my problem. It was a very careless oversight.
确保您还三重检查文档顶部是否有重复的 urlset 声明。这是我的问题。这是一个非常粗心的疏忽。
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9"
url="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">