将 xml/CDATA 嵌套到 xml 文件中?

发布于 2024-12-15 13:49:06 字数 550 浏览 0 评论 0 原文

我需要将一个相当大的 xml 文件放入另一个 xml 文件中。我考虑使用 CDATA 来实现此目的:

http://www. w3.org/TR/2000/REC-xml-20001006#sec-cdata-sect http://www.w3schools.com/xml/xml_cdata.asp

但由于我的 xml 也可能包含 CDATA 这不起作用,除非我做了一些令人讨厌的解决方法:

http://web-design.blogs.webucator.com/2010/11/20/nesting-cdata-blocks/

是否有更好的方法来传输/编码大型嵌套 xml 文件或者是xml 格式根本不适合以这种方式使用?

I need to put a rather large xml file into another xml file. I considered using CDATA for this:

http://www.w3.org/TR/2000/REC-xml-20001006#sec-cdata-sect
http://www.w3schools.com/xml/xml_cdata.asp

but since my xml might also contain CDATA this does not work unless I do some nasty workaround:

http://web-design.blogs.webucator.com/2010/11/20/nesting-cdata-blocks/

Are there better ways of transferring/encoding large nested xml files or is the xml format simply not meant to be used in this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕头说它不想醒 2024-12-22 13:49:06

您可以将内部的 ]] 替换为 ]]]]>

(基于 http://web-design.blogs.webucator.com/2010/11/20/nesting-cdata-blocks/

示例:我们有外部和内部文档,我们想要放置内部为CDATA,外部为CDATA。

<outer>
  <e1 name="abc"/>
  <innerDoc><![CDATA[
      <Doc1/>
    ]]></innerDoc>
</outer>

<inner>
  <innrer1/>
  <a><![CDATA[
      free text with << >
    ]]></a>
  <innrer2/>
</inner>

如果我们只是复制并粘贴,我们会得到一个无效的 xml

<outer>
  <e1 name="abc"/>
  <innerDoc><![CDATA[
      <inner>
        <innrer1/>
        <a><![CDATA[
            free text with << >
          ]]></a>
        <innrer2/>
      </inner>
    ]]></innerDoc>
</outer>

,但是,如果我们在嵌入之前将内部 ]] 替换为 ]]]]>

<outer>
  <e1 name="abc"/>
  <innerDoc><![CDATA[
      <inner>
        <innrer1/>
        <a><![CDATA[
            free text with << >
          ]]]]><![CDATA[></a>
        <innrer2/>
      </inner>
    ]]></innerDoc>
</outer>

you can replace the inner ]] with ]]]]><![CDATA[

(based on http://web-design.blogs.webucator.com/2010/11/20/nesting-cdata-blocks/ )

Example: We have outer and inner docs and we want to put the inner inside the outer as CDATA.

<outer>
  <e1 name="abc"/>
  <innerDoc><![CDATA[
      <Doc1/>
    ]]></innerDoc>
</outer>

<inner>
  <innrer1/>
  <a><![CDATA[
      free text with << >
    ]]></a>
  <innrer2/>
</inner>

if we just copy and paste we get an invalid xml

<outer>
  <e1 name="abc"/>
  <innerDoc><![CDATA[
      <inner>
        <innrer1/>
        <a><![CDATA[
            free text with << >
          ]]></a>
        <innrer2/>
      </inner>
    ]]></innerDoc>
</outer>

however, if we replace the inner ]] with ]]]]><![CDATA[ before embedding it we fix the problem

<outer>
  <e1 name="abc"/>
  <innerDoc><![CDATA[
      <inner>
        <innrer1/>
        <a><![CDATA[
            free text with << >
          ]]]]><![CDATA[></a>
        <innrer2/>
      </inner>
    ]]></innerDoc>
</outer>
逆光下的微笑 2024-12-22 13:49:06

是的,在最顶层文档中将 CDATA 部分设为数据类型 bin.base64。这样,即使您要包装的文档包含 CDATA 部分,您也将受到保护。作为额外的好处,您的应用程序还将支持二进制文件(图像、电子表格等)。

下面是一些基于 Microsoft ADO 和 MSXML 的代码。

function wrapBinaryFile( strFileName)
{
    var ado_stream = new ActiveXObject("ADODB.Stream");
    var xml = newXMLDocument();
    xml.loadXML("<file/>");
    xml.documentElement.setAttribute( "name", strFileName );

    xml.documentElement.setAttribute("xmlns:dt","urn:schemas-microsoft-com:datatypes");

    xml.documentElement.dataType = "bin.base64";
    ado_stream.Type = 1; // 1=adTypeBinary
    ado_stream.Open();
    ado_stream.LoadFromFile( strFileName );
    xml.documentElement.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
    ado_stream.Close();
    return xml;
}

以及如何解开另一端的包裹......

function unwrapBinaryFile(ndFile, strFileName )
{
    var ado_stream = new ActiveXObject("ADODB.Stream");
    ndFile.dataType = "bin.base64";

    ado_stream.Type = 1; // 1=adTypeBinary
    ado_stream.Open();
    ado_stream.write( ndFile.nodeTypedValue );
    ado_stream.SaveToFile( strFileName, 2 );
    ado_stream.Close(); 
}

Yes, in your top-most document make the CDATA section of data type bin.base64. That way even if the document you're wrapping contains a CDATA section, you're protected. As an added bonus, your application will also support binary files (images, spreadsheets, etc.).

Here's some code that does it, based on Microsoft ADO, and MSXML.

function wrapBinaryFile( strFileName)
{
    var ado_stream = new ActiveXObject("ADODB.Stream");
    var xml = newXMLDocument();
    xml.loadXML("<file/>");
    xml.documentElement.setAttribute( "name", strFileName );

    xml.documentElement.setAttribute("xmlns:dt","urn:schemas-microsoft-com:datatypes");

    xml.documentElement.dataType = "bin.base64";
    ado_stream.Type = 1; // 1=adTypeBinary
    ado_stream.Open();
    ado_stream.LoadFromFile( strFileName );
    xml.documentElement.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
    ado_stream.Close();
    return xml;
}

And how to un-wrap it on the other end...

function unwrapBinaryFile(ndFile, strFileName )
{
    var ado_stream = new ActiveXObject("ADODB.Stream");
    ndFile.dataType = "bin.base64";

    ado_stream.Type = 1; // 1=adTypeBinary
    ado_stream.Open();
    ado_stream.write( ndFile.nodeTypedValue );
    ado_stream.SaveToFile( strFileName, 2 );
    ado_stream.Close(); 
}
时光与爱终年不遇 2024-12-22 13:49:06

第一个 XML:

<root>
    <data1 value="test1" />
    <data2>
        <value>test2</value>
    </data2>
</root>

第二个 XML:

<root2>
    <data3 value="test3" />
    <data4>
        <value>test4</value>
    </data2>
</root2>

您可以使用特定节点将第二个 XML 包含在第一个 XML 中:

<root>
    <data1 value="test1" />
    <data2>
        <value>test2</value>
    </data2>
    <dataFromSecondXML>
        <data3 value="test3" />
        <data4>
            <value>test4</value>
        </data2>
    </dataFromSecondXML>
</root>

First XML :

<root>
    <data1 value="test1" />
    <data2>
        <value>test2</value>
    </data2>
</root>

Second XML :

<root2>
    <data3 value="test3" />
    <data4>
        <value>test4</value>
    </data2>
</root2>

You can include second XML in first with a specific node :

<root>
    <data1 value="test1" />
    <data2>
        <value>test2</value>
    </data2>
    <dataFromSecondXML>
        <data3 value="test3" />
        <data4>
            <value>test4</value>
        </data2>
    </dataFromSecondXML>
</root>
娇纵 2024-12-22 13:49:06

XML 是分层的:为什么不能在没有 CDATA 的情况下直接嵌套文档?除了 DTD 问题之外,任何 XML 文档都可以复制为另一个文档中元素的内容。

XML is hierarchic: why can't you nest the documents directly, without CDATA? Apart from DTD issues, any XML document can be copied as the content of an element in another document.

┾廆蒐ゝ 2024-12-22 13:49:06

简而言之,XML 不适合以这种方式使用!

但是,如果对要打包的 XML 文件进行 Base64 编码,则编码结果将不包含任何可能被解释为标记或实体引用的字符,并且可以安全地作为文本节点的内容保存。

The short answer is that XML is not meant to be used in this way!

However, if you base64-encode the XML file to be packaged, the encoded result will not contain any characters which might be interpreted either as markup, or as entity references, and can safely be held as the contents of a text node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文