Java JAXB Marshaller - 如何抑制 xmlns 命名空间属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9829277/
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
JAXB Marshaller - How do I suppress xmlns namespace attributes?
提问by arrehman
I generate XML using JAXB2 for an standalone java app that uses maven 3, jaxb2, FIXML schemas and maven-jaxb2-plugin. When I marshall the XML, the output root element has xmlns attributes in them. How do I remove this?
我使用 JAXB2 为使用 maven 3、jaxb2、FIXML 模式和 maven-jaxb2-plugin 的独立 Java 应用程序生成 XML。当我编组 XML 时,输出根元素中有 xmlns 属性。我如何删除它?
From:
从:
<root ... xmlns="http://www.fixprotocol.org/FIXML-4-4">...</root>
To:
到:
<root ... >...</root>
Edit:
编辑:
package-info.java
包信息.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.fixprotocol.org/FIXML-4-4", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.package;
采纳答案by arrehman
The problem is resolved for now by removed all references to the namespace "http://www.fixprotocol.org/FIXML-4-4" from the generated jaxb2 code. This is crude. I was hoping for a more automated way.
通过从生成的 jaxb2 代码中删除对名称空间“http://www.fixprotocol.org/FIXML-4-4”的所有引用,该问题暂时得到解决。这很粗糙。我希望有一种更自动化的方式。
回答by Chris White
So if you don't want the namespace information included, you could just remove the @XmlSchema
annotation from package-info.java
.
因此,如果您不想包含命名空间信息,则可以@XmlSchema
从package-info.java
.
This somewhat breaks your automated build though as you said this is auto-generated from the maven plugin.
尽管正如您所说,这是从 Maven 插件自动生成的,但这在某种程度上破坏了您的自动构建。
Suggest you look at the plugin options to see if you can remove this from the output, or you'll have to use some additional plugins to remove this line, or just delete the package-info.java all together.
建议您查看插件选项,看看是否可以从输出中删除它,或者您必须使用一些额外的插件来删除这一行,或者只是一起删除 package-info.java。
Question - are you going to be passing this XML to a service that expects the namespace information to be included? I guess i'm interested in why you want to strip off the namespace info.
问题 - 您是否要将此 XML 传递给需要包含名称空间信息的服务?我想我对您为什么要剥离名称空间信息感兴趣。
回答by Yevgeniy Kovaliov
In order to automate the process you can just remove attribute targetNamespacefrom source xsd file
为了自动化该过程,您可以从源 xsd 文件中删除属性targetNamespace