简体   繁体   中英

simple_xml parser error with quotation marks in xml, even with CDATA

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.

  1. I changed " into " in the XML-file --> parser-error.
  2. I escaped the " in the XML like \" --> parser-error.
  3. I used ' instead of " --> everything fine, but I want " !
  4. So I included the part with the "" 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 &#34; 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

&quot; , CDATA and &#34; 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 等效实体的引号: &#34;

str_replace('"', '&#34;', $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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM