XOM 和规范 XML

发布于 2024-11-15 11:40:38 字数 832 浏览 0 评论 0 原文

我正在制作一个java应用程序来检查XML文件是否已经规范或不使用XOM。

在我的测试中,我有以下文件,该文件已经是规范的。

<doc xmlns="http://example.com/default" xmlns:x="http://example.com/x">
  <a a1="1" a2="2">123</a>
  <b xmlns:y="http://example.com/y" a3="&quot;3&quot;" y:a1="1" y:a2="2"></b>
</doc>

这是我用 XOM 再次加载时的代码。

<?xml version="1.0"?>
<doc xmlns="http://example.com/default" xmlns:x="http://example.com/x">
  <a a1="1" a2="2">123</a>
  <b xmlns:y="http://example.com/y" a3="&quot;3&quot;" y:a1="1" y:a2="2" />
</doc>

正如您所看到的,它再次添​​加了 xml 标签并删除了结束标签 因为标签 b 的值为空。 我对 xml 版本标记没有任何问题,但当我从文件加载规范文档时,我不知道如何保留结束标记

I'm making a java application that checks if a XML file is already Canonical or not using XOM.

In my tests I have the following file which is already Canonical.

<doc xmlns="http://example.com/default" xmlns:x="http://example.com/x">
  <a a1="1" a2="2">123</a>
  <b xmlns:y="http://example.com/y" a3=""3"" y:a1="1" y:a2="2"></b>
</doc>

Here it is the code when I load it again with XOM.

<?xml version="1.0"?>
<doc xmlns="http://example.com/default" xmlns:x="http://example.com/x">
  <a a1="1" a2="2">123</a>
  <b xmlns:y="http://example.com/y" a3=""3"" y:a1="1" y:a2="2" />
</doc>

As you can see it adds again xml tag and delete the closing tag </b> because the value of tag b is empty.
I haven't got any problem with xml version tag but I don't know what to do to keep the closing tag </b> when I load the canonical document from file.

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

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

发布评论

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

评论(1

生活了然无味 2024-11-22 11:40:38

看起来您正在使用 XOM Serializer 输出文档,您需要使用 XOM Canonicalizer 输出您的 xml 文档并保留它Canonical

这给出了输出:

<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns="http://example.com/default" xmlns:x="http://example.com/x">
    <a a1="1" a2="2">123</a>
    <b a3=""3"" y:a1="1" y:a2="2" xmlns:y="http://example.com/y"/>
</doc>

以下示例程序将使用 XOM 规范化器

package com.foo.bar.xom;

import java.io.IOException;

import nu.xom.Builder;
import nu.xom.canonical.Canonicalizer;
import nu.xom.Document;
import nu.xom.ParsingException;
import nu.xom.Serializer;
import nu.xom.ValidityException;

public class App
{
    public static void main(String[] args) throws ValidityException, ParsingException, IOException
    {
        Builder builder = new Builder();
        //Serializer serializer = new Serializer(System.out);
        Canonicalizer canonicalizer = new Canonicalizer(System.out, Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
        //this assumes to your xml document is on the classpath in this package as my.xml
        Document input = builder.build(App.class.getResourceAsStream("my.xml"), null);
        //serializer.write(input);
        canonicalizer.write(input);

    }
}

It looks like you are outputting the document with a XOM Serializer you need to use a XOM Canonicalizer to output your xml document and keep it Canonical

This gives the output:

<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns="http://example.com/default" xmlns:x="http://example.com/x">
    <a a1="1" a2="2">123</a>
    <b a3=""3"" y:a1="1" y:a2="2" xmlns:y="http://example.com/y"/>
</doc>

The following example program will output your XML Cannonically to System.out using a XOM Canonicalizer

package com.foo.bar.xom;

import java.io.IOException;

import nu.xom.Builder;
import nu.xom.canonical.Canonicalizer;
import nu.xom.Document;
import nu.xom.ParsingException;
import nu.xom.Serializer;
import nu.xom.ValidityException;

public class App
{
    public static void main(String[] args) throws ValidityException, ParsingException, IOException
    {
        Builder builder = new Builder();
        //Serializer serializer = new Serializer(System.out);
        Canonicalizer canonicalizer = new Canonicalizer(System.out, Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
        //this assumes to your xml document is on the classpath in this package as my.xml
        Document input = builder.build(App.class.getResourceAsStream("my.xml"), null);
        //serializer.write(input);
        canonicalizer.write(input);

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