I'm reading XML with simplexml_load_string()
from array (from database).
Everything is fine until the XML contains quotation marks, simplexml starts to report XML parse errors.
"
into "
in the XML-file --> parser-error."
in the XML like \"
--> parser-error.'
instead of "
--> everything fine, but I want "
!""
into CDATA
--> parser-error again!`XML-snippet:
<prepare var="%vorfall%" label="Bezeichnen Sie den Vorfall">
<![CDATA["Vorfall beim Reiten..."]]>
</prepare>
PHP-snippet:
$rxml=simplexml_load_string(utf8_encode($test['tsValues']));
errors - with "
instead of "
:
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
UPDATE: checked again, problem is happening here - 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
"
, CDATA
and "
do work with simplexml as intended.
My mistake: the code replaced a substr %vorfall%
within a xml-file. This substring occured within the text of a node (replacement ok) and within an attribute var="%vorfall%"
. The parse-error happened when the attribute had double ""
: var=""some-replaced-content""
.
Thank you all for your help!
尝试替换其 XML 等效实体的引号: "
str_replace('"', '"', $xml);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.