Java Jaxb 生成的 xml - 根元素前缀问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4312572/
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 generated xml - problem with root element prefix
提问by crazyTechie
I am trying to generate xml using jaxb. I created xsd and generated java classes. But when I generate xml, I am geeting prefix ns2 to the root tag, which I don't want.
我正在尝试使用 jaxb 生成 xml。我创建了 xsd 并生成了 java 类。但是,当我生成 xml 时,我将前缀 ns2 添加到根标记中,这是我不想要的。
ex: I want root tag to be
例如:我想要根标签
<report>
<id>rep 1</id>
</report>
, But getting as
, 但得到
<ns2:report>
....
</ns2:report>
In the generated java class, I gave annotation as @XmlRootElement(name="report",namespace="urn:report")
在生成的java类中,我给出了注释为 @XmlRootElement(name="report",namespace="urn:report")
Can some one pls help
有人可以帮忙吗
采纳答案by bdoughan
If this is your class:
如果这是您的课程:
package example;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="report",namespace="urn:report")
public class Root {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Then it makes sense that there is a prefix on the root element, because you have specified that the "root" element is namespace qualified and the "id" element is not.
那么在根元素上有一个前缀是有道理的,因为您已经指定“root”元素是命名空间限定的,而“id”元素不是。
<ns2:report xmlns:ns2="urn:report">
<id>123</id>
</ns2:report>
If you add a package-info class to your model, you can leverate the @XmlSchema annotation:
如果向模型添加 package-info 类,则可以利用 @XmlSchema 注释:
@XmlSchema(
namespace = "urn:report",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Then the JAXB implementation may choose to leverage the default namespace, but note now all of the elements are namespace qualified which may or may not match your XML schema:
然后 JAXB 实现可能会选择利用默认命名空间,但请注意,现在所有元素都是命名空间限定的,它们可能与您的 XML 模式匹配,也可能不匹配:
<report xmlns="urn:report">
<id>123</id>
</report>
For more information on JAXB and namespaces see:
有关 JAXB 和命名空间的更多信息,请参阅:
回答by stacker
The blog entry Customizing JAXBshows the alternatives provided by implementing a PreferredMapper
. Unfortunately it explains, that is not possible to fully suppress namespaces.
博客条目Customizing JAXB展示了通过实现PreferredMapper
. 不幸的是,它解释说,完全抑制命名空间是不可能的。
回答by dogbane
Take a look at this answer. It describes how to use a SAX Filter to remove any namespace.
看看这个答案。它描述了如何使用 SAX 过滤器删除任何命名空间。
回答by MeplatMasher
Use this attribute in your root element of your schema: elementFormDefault="qualified" So for instance:
在架构的根元素中使用此属性: elementFormDefault="qualified" 例如:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
回答by Carlos H. Raymundo
Somehow the accepted answer did not work for me. I got success when I found solutions in some related stockOverflow questions involving DelegatingXMLStreamWriterfrom cxf and a filter, NoNamesWriter. The implementation I used with NoNamesWriter:
不知何故,接受的答案对我不起作用。当我在一些涉及DelegatingXMLStreamWriterfrom cxf 和过滤器NoNamesWriter 的相关 stockOverflow 问题中找到解决方案时,我获得了成功。我与 NoNamesWriter 一起使用的实现:
public class NoNamesWriter extends DelegatingXMLStreamWriter
{
@Override
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
delegate.writeStartElement("", local, uri);
}
public static XMLStreamWriter filter(FileOutputStream fileOutputStream) throws XMLStreamException {
return new NoNamesWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream));
}
public NoNamesWriter(XMLStreamWriter writer) {
super(writer);
}
}
Invoke the same as described here, like:
调用与此处描述的相同,例如:
xmlmarshaller.marshal(xc, NoNamesWriter.filter(new FileOutputStream(outfile, false));
xmlmarshaller.marshal(xc, NoNamesWriter.filter(new FileOutputStream(outfile, false));