simple_xml解析器错误,带有xml中的引号,即使使用CDATA也是如此

问题描述 投票:0回答:2

我正在从数组(来自数据库)中使用simplexml_load_string()读取XML。

一切都很好,直到XML包含引号,simplexml开始报告XML解析错误。

  1. 我在XML文件 - > parser-error中将"更改为"
  2. 我在" - > parser-error中逃脱了XML中的\"
  3. 我使用'而不是" - >一切都很好,但我想要"
  4. 所以我把""的部分包含在CDATA中 - >解析器错误了!

XML - 摘录:

<prepare var="%vorfall%" label="Bezeichnen Sie den Vorfall">
    <![CDATA["Vorfall beim Reiten..."]]>
</prepare>

PHP - 摘录:

$rxml=simplexml_load_string(utf8_encode($test['tsValues']));

错误 - 与&#34;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 simplexml php
2个回答
0
投票

尝试替换其XML等效实体的引号:qazxsw poi

&#34;

0
投票

str_replace('"', '&#34;', $xml); &quot;CDATA可以按照预期使用simplexml。

我的错误:代码替换了xml文件中的substr &#34;。此子字符串出现在节点文本(替换ok)和属性%vorfall%中。当属性有双var="%vorfall%"""时发生解析错误。

感谢大家的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.