如何包含和读取 XML 的 HTML?
<myxml>
<bla>
I want <strong>HTML here</strong>
</bla>
</myxml>
如何从 XML 文档中读取 HTML?
$data = file_get_contents('myxml.xml');
$xml = new SimpleXMLElement($data);
print_r($xml); // fail...
ps:没有转义,因为每次我添加一些东西时转义文本很烦人..
编辑:
<myxml>
<bla><![CDATA[I want <strong>HTML here</strong>]]></bla>
</myxml>
PHP:
$xml = simplexml_load_file('myxml.xml');
print_r($xml);
并且输出是:
SimpleXMLElement Object
(
[bla] => SimpleXMLElement Object
(
)
)
那里没有cdata..
<myxml>
<bla>
I want <strong>HTML here</strong>
</bla>
</myxml>
How can I read the HTML from the XML document?
$data = file_get_contents('myxml.xml');
$xml = new SimpleXMLElement($data);
print_r($xml); // fail...
ps: without escaping, because it's annoying to escape the text each time I add something..
edit:
<myxml>
<bla><![CDATA[I want <strong>HTML here</strong>]]></bla>
</myxml>
the PHP:
$xml = simplexml_load_file('myxml.xml');
print_r($xml);
and the output is:
SimpleXMLElement Object
(
[bla] => SimpleXMLElement Object
(
)
)
no cdata there..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用
CDATA
标签,如下所示:这将告诉解析器忽略 CDATA 块内的内容,并将其解析为纯文本。
Surround your
I want <strong>HTML here</strong>
withCDATA
tags, as such:This will tell parsers to ignore what's inside the CDATA block and just parse it as plaintext.
文本节点不需要显式类型转换,并且它仅在此处起作用;
print_r()
是此处使用的错误函数/语言功能(请使用echo
代替)。要将任何 SimpleXML 元素的内容打印为 XML(也可能包含其他元素),请使用其 asXML() 方法。
Explicit typecast is not required for text nodes, and it works only there;
print_r()
is the wrong function/language feature to use here (useecho
instead).For printing the content of any SimpleXML element as XML (one that may also contain other elements), use its asXML() method.