删除“独立=”是“” 从生成的 XML

发布于 2024-07-08 19:58:52 字数 158 浏览 14 评论 0原文

您是否知道 JAXB 设置可以防止在结果 XML 中生成 standalone="yes"

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

Do you know of a JAXB setting to prevent standalone="yes" from being generated in the resulting XML?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

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

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

发布评论

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

评论(15

↘紸啶 2024-07-15 19:58:53
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

这对我的 JDK1.7 有用。 standalone=\"no\" 可以删除以仅获取 xml 部分的其余部分

jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

This worked for me with JDK1.7. standalone=\"no\" can be removed to get only rest of the xml part

阳光的暖冬 2024-07-15 19:58:53

如果您仅使用默认的 javax.xml 包,则可以将编组器的 JAXB_FRAGMENT 选项设置为“true”(这会忽略默认的 xml 处理指令)并使用 XMLStreamWriter 的 writeProcessingInstruction 方法插入您自己的:

xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();

If you are using only the default javax.xml package, you could set the JAXB_FRAGMENT option of the marshaller to 'true' (this omits the default xml processing instruction) and use the writeProcessingInstruction method of the XMLStreamWriter to insert your own:

xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
蹲墙角沉默 2024-07-15 19:58:53

你试一试

private String marshaling2(Object object) throws JAXBException, XMLStreamException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    StringWriter writer = new StringWriter();
    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    jaxbMarshaller.marshal(object, writer);
    return writer.toString();
  }

just try

private String marshaling2(Object object) throws JAXBException, XMLStreamException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    StringWriter writer = new StringWriter();
    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    jaxbMarshaller.marshal(object, writer);
    return writer.toString();
  }
你的他你的她 2024-07-15 19:58:53

我正在使用 Java 1.8 和 JAXB 2.3.1

首先,请确保在 pom.xml 中使用 java 1.8

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

然后在源代码中我使用:(关键是内部部分)

// remove standalone=yes
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

I'm using Java 1.8 and JAXB 2.3.1

First, be sure to be using java 1.8 in pom.xml

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

Then in source code I used: (the key was the internal part)

// remove standalone=yes
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
倦话 2024-07-15 19:58:53

您可以使用:
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);

它在 Java 8 上对我有用

You can use:
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);

It works for me on Java 8

北笙凉宸 2024-07-15 19:58:53

我没有足够高的“声誉”来拥有发表评论的“特权”。 ;-)

@Debasis,请注意您指定的属性:

"com.sun.xml.internal.bind.xmlHeaders"

应该是:

"com.sun.xml.bind.xmlHeaders" (without the "internal", which are not meant to be used by the public)

如果我像您一样使用“内部”属性,我会得到一个 javax.xml.bind.PropertyException

I don't have a high enough "reputation" to have the "privilege" to comment. ;-)

@Debasis, note that the property you've specified:

"com.sun.xml.internal.bind.xmlHeaders"

should be:

"com.sun.xml.bind.xmlHeaders" (without the "internal", which are not meant to be used by the public)

If I use the "internal" property as you did, I get a javax.xml.bind.PropertyException

雾里花 2024-07-15 19:58:53

如果您遇到属性异常,请添加以下配置:

jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders",
              "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlDeclaration", Boolean.FALSE);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);  

In case you are getting property exception, add the following configuration:

jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders",
              "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlDeclaration", Boolean.FALSE);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);  
爱,才寂寞 2024-07-15 19:58:53

如果您有

但想要这样:

只需执行以下操作:

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

If you have <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

but want this: <?xml version="1.0" encoding="UTF-8"?>

Just do:

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
把梦留给海 2024-07-15 19:58:53

我在使用时遇到了 PropertyException:

marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

相反,对我有帮助的是将 JAXB_FRAGMENT 设置为 true,然后使用以下命令编写自定义标头:

marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE note SYSTEM>"

I was getting PropertyException when using:

marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

Instead what helped me was setting JAXB_FRAGMENT to true and then writing the custom header using:

marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE note SYSTEM>"
情绪失控 2024-07-15 19:58:53

这是 Java 17 / Java 21 的更新答案(使用 JAXB 的 Eclipse Jakarta 参考实现)。

我在 POM 中的依赖项如下所示(对于 Java SE 应用程序):

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-bom</artifactId>
                <version>4.0.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- JAXB API -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>

        <!-- JAXB Implementation (needed when not executing in a Java EE server) -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

根据找到的文档,相关属性现在名为 org.glassfish.jaxb.xmlHeaders 此处

所以,你会这样做:

String customXmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", customXmlHeader);

Here's an updated answer for Java 17 / Java 21 (using the Eclipse Jakarta reference implementation of JAXB).

My dependencies in POM look like this (for a Java SE application):

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-bom</artifactId>
                <version>4.0.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- JAXB API -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>

        <!-- JAXB Implementation (needed when not executing in a Java EE server) -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

The relevant property is nowadays named org.glassfish.jaxb.xmlHeaders as per the documentation found HERE.

So, you would do:

String customXmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", customXmlHeader);
红尘作伴 2024-07-15 19:58:52

在 JAXB 中,它是 JDK1.6 的一部分

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

in JAXB that is part of JDK1.6

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
迷你仙 2024-07-15 19:58:52

此属性:

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);

...可以用于没有:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

但是,我不认为这是最佳实践。

This property:

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);

...can be used to have no:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

However, I wouldn't consider this best practice.

帅气称霸 2024-07-15 19:58:52

您可以使用

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false)

禁用默认 XML 声明,然后将自定义 XML 声明添加

<?xml version="1.0" encoding="UTF-8"?>

marshaller.setProperty("com.sun.xml.bind.xmlHeaders",
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

生成的 xml 中,从而避免使用 standalone="yes" 属性。

You can either use

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

or

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false)

to disable the default XML declaration, and then add your custom XML declaration,

<?xml version="1.0" encoding="UTF-8"?>

by

marshaller.setProperty("com.sun.xml.bind.xmlHeaders",
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

to the generated xml, thus avoiding the standalone="yes" property.

陪我终i 2024-07-15 19:58:52

如果其他人仍然在解决这个问题,您可以考虑使用

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

删除所有 XML 声明,并在输出流/方法的开头编写您自己的 String

just if someone else is still struggeling with this problem, you may consider using

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

to remove all of the XML declaration and just write your own String at the beginning of your output stream / method

勿挽旧人 2024-07-15 19:58:52

如果您使文档依赖于DOCTYPE(例如使用命名实体),那么它将不再是独立的,因此在XML 声明中将不允许standalone="yes"

然而,独立的 XML 可以在任何地方使用,而非独立的 XML 解析器对于不加载外部的 XML 解析器来说是有问题的。

除了与不支持 XML 的软件的互操作性,以及一些可怕的正则表达式汤之外,我不明白这个声明怎么会成为问题。

If you make document dependent on DOCTYPE (e.g. use named entities) then it will stop being standalone, thus standalone="yes" won't be allowed in XML declaration.

However standalone XML can be used anywhere, while non-standalone is problematic for XML parsers that don't load externals.

I don't see how this declaration could be a problem, other than for interoperability with software that doesn't support XML, but some horrible regex soup.

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