Jaxb java 将命名空间设置为所有元素
我有一个Java类:
@XmlRootElement(name = "a")
public class a {
@XmlElementWrapper
public Component[] components;
public String content;
}
和一个XML文件:test.xml
和XSD文件,该文件是使用jaxbcontext :: GeneratesChema
方法生成的。
一个示例XML文件:
<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://example.com"
xsi:schemaLocation="http://example.com file://...schema.xsd">
</a>
如果我想解散它,我需要将namespace =“ http://example.com”添加到所有元素中。
否则,我将获得javax.xml.bind.unmarshalexception
as this 。
但是,如果我在许多不同的软件包中有很多类,或者某些带有@xmlelements注释的字段
有很多@xmlelement
children。
我需要复制和粘贴namespace =“ http://example.com”
无数的时间,它是愚蠢而毫无意义的。是否有一些方法可以让我只输入一次名称空间?
I have a Java class:
@XmlRootElement(name = "a")
public class a {
@XmlElementWrapper
public Component[] components;
public String content;
}
And an XML file: test.xml
and XSD file which is generated using the JAXBContext::generateSchema
method.
An example XML file:
<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://example.com"
xsi:schemaLocation="http://example.com file://...schema.xsd">
</a>
If I want to unmarshal it, I need to add namespace = "http://example.com" to all elements.
Otherwise, I will get a javax.xml.bind.UnmarshalException
as this.
However, If I have a lot of classes in many different packages, or some fields that are annotated with @XmlElements
has a lot of @XmlElement
children.
I need to copy and paste namespace = "http://example.com"
uncountable times, it is dumb and meaningless. Are there some methods that allow me to only type the namespace once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将文件:package-info.java添加到包装中。
You can try to add file: package-info.java to your package.