Jaxb生成了xml - 根元素前缀的问题 [英] Jaxb generated xml - problem with root element prefix
问题描述
我正在尝试使用jaxb生成xml。我创建了xsd并生成了java类。
但是当我生成xml时,我将前缀ns2加入到根标签中,这是我不想要的。
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.
ex:我希望root标签是
ex: I want root tag to be
<report>
<id>rep 1</id>
</report>
,但得到
<ns2:report>
....
</ns2:report>
在生成的java类中,我给出了注释为 @XmlRootElement(name = report,namespace =urn:report)
In the generated java class, I gave annotation as @XmlRootElement(name="report",namespace="urn:report")
有人可以帮助
推荐答案
如果这是你的班级:
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;
}
}
那么有意义的是根元素上的前缀,因为您已指定root元素是名称空间限定而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.
<ns2:report xmlns:ns2="urn:report">
<id>123</id>
</ns2:report>
如果将package-info类添加到模型中,则可以显示@XmlSchema注释:
If you add a package-info class to your model, you can leverate the @XmlSchema annotation:
@XmlSchema(
namespace = "urn:report",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
然后JAXB实现可以选择利用默认命名空间,但请注意现在所有元素都是命名空间与您的XML架构匹配或不匹配的限定条件:
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:
<report xmlns="urn:report">
<id>123</id>
</report>
有关JAXB和命名空间的更多信息,请参阅:
For more information on JAXB and namespaces see:
- http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html
这篇关于Jaxb生成了xml - 根元素前缀的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!