duanbianweng5353 2014-07-18 11:04
浏览 74
已采纳

XML:即使在转义字符串时也无法加载

I have big problem, I cant load XML file even when every special char is escaped. My XML:

    <code1>
    <code>&lt;?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, &quot;example.com&quot;);

        //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);
?&gt;</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.

  • 写回答

1条回答 默认 最新

  • duanbushi1867 2014-07-19 06:32
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记