問題描述
我正在嘗試使用 PHP 解析 XML 文件,但收到一條錯誤消息:
I'm trying to parse an XML file using PHP, but I get an error message:
解析器錯誤:字符 0x0 超出允許范圍
parser error : Char 0x0 out of allowed range in
我認為是因為XML的內(nèi)容,我認為有一個特殊的符號☆",有什么想法可以解決嗎?
I think it's because of the content of the XML, I think there is a speical symbol "☆", any ideas what I can do to fix it?
我也得到:
解析器錯誤:標簽項行中的數(shù)據(jù)過早結(jié)束
parser error : Premature end of data in tag item line
可能導致該錯誤的原因是什么?
What might be causing that error?
我正在使用 simplexml_load_file
一>.
I'm using simplexml_load_file
.
我嘗試找到錯誤行并將其內(nèi)容粘貼為單個 xml 文件,它可以工作!!所以我仍然無法弄清楚是什么導致 xml 文件解析失敗.PS 超過100M的超大xml文件,會不會導致解析錯誤?
I try to find the error line and paste its content as single xml file and it can work!! so I still cannot figure out what makes xml file parse fails. PS it's a huge xml file over 100M, will it makes parse error?
推薦答案
您是否可以控制 XML?如果是,請確保數(shù)據(jù)包含在 ..
]]>
塊中.
Do you have control over the XML? If so, ensure the data is enclosed in <![CDATA[
.. ]]>
blocks.
而且你還需要清除無效字符:
And you also need to clear the invalid characters:
/**
* Removes invalid XML
*
* @access public
* @param string $value
* @return string
*/
function stripInvalidXml($value)
{
$ret = "";
$current;
if (empty($value))
{
return $ret;
}
$length = strlen($value);
for ($i=0; $i < $length; $i++)
{
$current = ord($value[$i]);
if (($current == 0x9) ||
($current == 0xA) ||
($current == 0xD) ||
(($current >= 0x20) && ($current <= 0xD7FF)) ||
(($current >= 0xE000) && ($current <= 0xFFFD)) ||
(($current >= 0x10000) && ($current <= 0x10FFFF)))
{
$ret .= chr($current);
}
else
{
$ret .= " ";
}
}
return $ret;
}
這篇關(guān)于如何使用 PHP 跳過 XML 文件中的無效字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!