我正在从数组(来自数据库)中使用simplexml_load_string()
读取XML。
一切都很好,直到XML包含引号,simplexml开始报告XML解析错误。
"
更改为"
。"
- > parser-error中逃脱了XML中的\"
。'
而不是"
- >一切都很好,但我想要"
!""
的部分包含在CDATA
中 - >解析器错误了!XML - 摘录:
<prepare var="%vorfall%" label="Bezeichnen Sie den Vorfall">
<![CDATA["Vorfall beim Reiten..."]]>
</prepare>
PHP - 摘录:
$rxml=simplexml_load_string(utf8_encode($test['tsValues']));
错误 - 与"
instead "
:
Warning: simplexml_load_string(): Entity: line 16: parser error : attributes construct error in /Applications/MAMP/htdocs/_testcenter/testcenter.php on line 97
Warning: simplexml_load_string(): <prepare var=""Unfall beim Reiten, Rocky geht durch"" label="Bezeichnen Sie den in /Applications/MAMP/htdocs/_testcenter/testcenter.php on line 97
Warning: simplexml_load_string(): ^ in /Applications/MAMP/htdocs/_testcenter/testcenter.php on line 97
Warning: simplexml_load_string(): Entity: line 16: parser error : Couldn't find end of Start Tag prepare line 16 in /Applications/MAMP/htdocs/_testcenter/testcenter.php on line 97
Warning: simplexml_load_string(): <prepare var=""Unfall beim Reiten, Rocky geht durch"" label="Bezeichnen Sie den in /Applications/MAMP/htdocs/_testcenter/testcenter.php on line 97
Warning: simplexml_load_string(): ^ in /Applications/MAMP/htdocs/_testcenter/testcenter.php on line 97
更新:再次检查,问题发生在这里 - PHP:
$txml=file_get_contents("$path_test/".$test['tbTestFile']);
$rxml=simplexml_load_string($test['tsValues']);
// replacing parts of $txml with content containing the famous "" from $rxml
foreach ($rxml->xpath('//prepare') as $prep) {
$txml=str_replace($prep['var'],$prep,$txml);
}
$txml=simplexml_load_string($txml); // this is line 97
尝试替换其XML等效实体的引号:qazxsw poi
"
str_replace('"', '"', $xml);
,"
和CDATA
可以按照预期使用simplexml。
我的错误:代码替换了xml文件中的substr "
。此子字符串出现在节点文本(替换ok)和属性%vorfall%
中。当属性有双var="%vorfall%"
:""
时发生解析错误。
感谢大家的帮助!