Jaxb java 将命名空间设置为所有元素

发布于 2025-01-19 09:37:20 字数 864 浏览 2 评论 0原文

我有一个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 技术交流群。

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

发布评论

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

评论(1

红尘作伴 2025-01-26 09:37:20

您可以尝试将文件:package-info.java添加到包装中。

@javax.xml.bind.annotation.XmlSchema(namespace = 
"http://webservices.com/myws",
   xmlns = { 
       @javax.xml.bind.annotation.XmlNs(prefix = "ws", 
       namespaceURI="http://webservices.com/myws")
   } 
)
package mypackage;

You can try to add file: package-info.java to your package.

@javax.xml.bind.annotation.XmlSchema(namespace = 
"http://webservices.com/myws",
   xmlns = { 
       @javax.xml.bind.annotation.XmlNs(prefix = "ws", 
       namespaceURI="http://webservices.com/myws")
   } 
)
package mypackage;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文