在 XML 中包装任意 XML [英] Wrapping Arbitrary XML within XML

查看:36
本文介绍了在 XML 中包装任意 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在包装器 XML 文档中嵌入任意(语法上有效)的 XML 文档.嵌入的文档将被视为纯文本,在解析包装文档时它们不需要可解析.

I need to embed arbitrary (syntactically valid) XML documents within a wrapper XML document. The embedded documents are to be regarded as mere text, they do not need to be parseable when parsing the wrapper document.

我知道CDATA技巧",但如果内部 XML 文档本身包含 CDATA 段,我就不能使用它,并且我需要能够嵌入任何有效的 XML 文档.任何有关完成此操作或解决 CDATA 限制的建议都将不胜感激.

I know about the "CDATA trick", but I can't use that if the inner XML document itself contains a CDATA segment, and I need to be able to embed any valid XML document. Any advice on accomplishing this--or working around the CDATA limitation--would be appreciated.

推荐答案

您需要正确转义文本.您没有说明您使用的是什么语言,但一般来说:您构建一个 DOM,创建一个包含内部"XML 的 Text 节点,然后序列化该 DOM.序列化程序将为您处理转义.

You need to properly escape the text. You don't say what language you're using, but generally: you build a DOM, create a Text node that contains your "inner" XML, and then serialize that DOM. The serializer will handle escaping for you.

这里的关键是使用序列化程序来生成您的输出.不要简单地编写字符串,因为您几乎可以肯定会生成一些格式不正确的 XML.

The key point here is use a serializer to produce your output. Don't simply write strings, because you're all but guaranteed to produce something that's not well-formed XML.

这篇关于在 XML 中包装任意 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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