是否可以在 XML 文档中插入 HTML 内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4412395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:40:39  来源:igfitidea点击:

Is it possible to insert HTML content in XML document?

htmlxml

提问by newbie

I need to insert HTML content into an XML document, is this possible or should HTML content be, for example, encoded in BASE64 or with something else like that?

我需要将 HTML 内容插入到 XML 文档中,这是可能的还是应该将 HTML 内容编码为 BASE64 或其他类似内容?

回答by Pablo Santa Cruz

You can include HTML content. One possibility is encoding it in BASE64 as you have mentioned.

您可以包含 HTML 内容。正如您所提到的,一种可能性是在 BASE64 中对其进行编码。

Another might be using CDATAtags.

另一个可能是使用CDATA标签。

Example using CDATA:

使用示例CDATA

<xml>
    <title>Your HTML title</title>
    <htmlData><![CDATA[<html>
        <head>
            <script/>
        </head>
        <body>
        Your HTML's body
        </body>
        </html>
     ]]>
    </htmlData>
</xml>

Please note:

请注意:

CDATA's opening character sequence: <![CDATA[

CDATA 的开头字符序列: <![CDATA[

CDATA's closing character sequence: ]]>

CDATA 的结束字符序列: ]]>

回答by zzzzBov

so long as your html content doesn't need to contain a CDATAelement, you can contain the HTML in a CDATAelement, otherwise you'll have to escape the XML entities.

只要您的 html 内容不需要包含CDATA元素,您就可以在CDATA元素中包含 HTML ,否则您将不得不转义 XML 实体。

<element><![CDATA[<p>your html here</p>]]></element>

VS

VS

<element>&lt;p&gt;your html here&lt;/p&gt;</element>

回答by Rich

The purpose of BASE64 encoding is to take binary data and be able to persist that to a string. That benefit comes at a cost, an increase in the size of the result (I think it's a 4 to 3 ratio). There are two solutions. If you know the data will be well formed XML, include it directly. The other, an better option, is to include the HTML in a CDATA section within an element within the XML.

BASE64 编码的目的是获取二进制数据并将其保存为字符串。这种好处是有代价的,即结果大小的增加(我认为这是一个 4 比 3 的比例)。有两种解决方案。如果您知道数据将是格式良好的 XML,请直接包含它。另一种更好的选择是将 HTML 包含在 XML 中元素内的 CDATA 部分中。

回答by loyola

Please see this.

请看这个。

Text inside a CDATA section will be ignored by the parser.

CDATA 部分内的文本将被解析器忽略。

http://www.w3schools.com/xml/dom_cdatasection.asp

http://www.w3schools.com/xml/dom_cdatasection.asp

This is will help you to understand the basics about XML

这将帮助您了解有关 XML 的基础知识