I have big problem, I cant load XML file even when every special char is escaped. My XML:
<code1>
<code><?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?></code>
</code1>
When I insert in <code></code>
another string, ex. Hello world
, it works. Why it doesnt work? BTW, I am using PHP's simplexml_load_file
. Thank you.
By wrapping the contents in a CDATA block, you can make the XML parser (PHP) ignore the contents. By calling simplexml_load_file
with LIBXML_NOCDATA
as the third argument, PHP will return the contents within the CDATA block, but will not interfere with the contents (escaping characters).
So, the solution is wrapping the XML contents in a CDATA-block:
<code1>
<code>
<![CDATA[contents with special characters]]>
</code>
</code1>
and load the file in PHP by using:
$xml = simplexml_load_file($file_xml, null, LIBXML_NOCDATA);