XML内的HTML。我应该使用CDATA还是对HTML进行编码 [英] Html inside XML. Should I use CDATA or encode the HTML

查看:55
本文介绍了XML内的HTML。我应该使用CDATA还是对HTML进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XML共享HTML内容。 AFAIK,我可以通过以下方式嵌入HTML:

I am using XML to share HTML content. AFAIK, I could embed the HTML either by:


  • 对其进行编码:我不知道使用它是否完全安全。并且我将不得不再次对其进行解码。

  • Encoding it: I don't know if it is completely safe to use. And I would have to decode it again.

使用CDATA部分:如果内容包含结束标记]]>和某些内容,我仍然会遇到问题我相信,十六进制字符。另一方面,XML解析器会为我透明地提取信息。

Use CDATA sections: I could still have problems if the content contains the closing tag "]]>" and certain hexadecimal characters, I believe. On the other hand, the XML parser would extract the info transparently for me.

我应该选择哪个选项?

更新:xml将用Java创建,并作为字符串传递给.net Web服务,否则将被解析。因此,我需要能够将xml导出为字符串并使用 doc.LoadXml(xmlString);

UPDATE: The xml will be created in java and passed as a string to a .net web service, were it will be parsed back. Therefore I need to be able to export the xml as a string and load it using "doc.LoadXml(xmlString);"

推荐答案

这两个选项几乎完全相同。这是您的两个选择:

The two options are almost exactly the same. Here are your two choices:

<html>This is &lt;b&gt;bold&lt;/b&gt;</html>

<html><![CDATA[This is <b>bold</b>]]></html>

在两种情况下,您都必须检查字符串中是否要转义特殊字符。很多人假装CDATA字符串不需要任何转义,但正如您指出的那样,您必须确保]]>不会滑入未转义的位置。

In both cases, you have to check your string for special characters to be escaped. Lots of people pretend that CDATA strings don't need any escaping, but as you point out, you have to make sure that "]]>" doesn't slip in unescaped.

在两种情况下,XML处理器都会将您的字符串返回给您解码。

In both cases, the XML processor will return your string to you decoded.

这篇关于XML内的HTML。我应该使用CDATA还是对HTML进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆