如何使用JAXB将多个名称空间编组到XML根元素 [英] How to marshal multiple namespaces to XML root element with JAXB

查看:106
本文介绍了如何使用JAXB将多个名称空间编组到XML根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JAXB封送到xml,其中命名空间仅出现在根元素中而不出现在任何其他元素中:

I would like to have JAXB marshal to xml where the namespaces appear ONLY in the root element and not in any others:

<myroot xmlns="http://www.mysite.com" xmlns:a="http://www.mysite.com/a" xmlns:ab="http://www.mysite.com/ab" xmlns:y="http://www.mysite.com/y">
...
</myroot>

我已经尝试了包级别@XmlSchema,但我必须做错事,因为没有任何显示。

I've tried package level @XmlSchema, but I must be doing something wrong because nothing shows up.

这是我的根元素类:

@XmlRootElement(name="myroot")
@XmlAccessorType(XmlAccessType.FIELD)
public class RootElementClass
{
....
}

我还有以下package-info.java:

I also have the following package-info.java:

@XmlSchema
(
    namespace="http://www.mysite.com", 
    elementFormDefault=XmlNsForm.QUALIFIED,
    xmlns=
    {
            @XmlNs(namespaceURI = "http://www.mysite.com",    prefix = ""),
            @XmlNs(namespaceURI = "http://www.mysite.com/a",  prefix = "a"),
            @XmlNs(namespaceURI = "http://www.mysite.com/ab", prefix = "ab"),
            @XmlNs(namespaceURI = "http://www.mysite.com/y",  prefix = "y")
    }
)
package com.seastreetinc.rd.nso.jaxb;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;


推荐答案

JAXB规范说什么(并不说)



JAXB(JSR-222)规范未涵盖命名空间声明发生的位置或调用前缀的位置(请参阅: http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html )。

实现使用的一般策略是确定命名空间的最小数量由模型使用并在根元素上声明它们。这可能是您在 @XmlSchema 注释中声明的内容的一部分。

The general strategy used by implementations is to figure out the minimum amount of namespaces used by the model and declare them on the root element. This could be a subset of what you have declared in the @XmlSchema annotation.

NamespacePrefixMapper 扩展名可用于控制根元素上的命名空间。请参阅下面链接的答案以获取完整示例。

The NamespacePrefixMapper extension can be used to control the namespaces on the root element. See the answer linked below for a full example.

  • JAXB - Move dynamically generated namespaces to document root

这篇关于如何使用JAXB将多个名称空间编组到XML根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆