如何使用JAXB设置默认命名空间 [英] How to set the default namespace using JAXB

查看:676
本文介绍了如何使用JAXB设置默认命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过Spring MVC Web服务返回的数据的ATOM-XML表示。我正在使用JAXB进行序列化,我有许多名称空间但我希望默认名称空间设置为Atom而没有前缀。以下是我目前在 package-info.java 中的内容,但原子前缀设置为ns3。

I have a ATOM-XML representation of my data that is returned via a Spring MVC web service. I'm using JAXB to do the serialization, I have a number of namespaces but I want the default namespace set to Atom with no prefix. Here is what I have so far in package-info.java but the atom prefix is being set to ns3.

@XmlSchema(namespace = com.mycomponay.foo.ATOM_NAMESPACE,
xmlns = { 
    @XmlNs(prefix = "foo", namespaceURI = com.mycomponay.foo.NAMESPACE_FOO),
}, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.mycompany.web;

import javax.xml.bind.annotation.XmlNs;

此外,我注意到命名空间显示在chrome中,但不显示在Firefox中。

Also I noticed the namespaces display in chrome but not in Firefox.

推荐答案

尝试添加 @XmlNs 注释,前缀为用于要显示为默认名称的命名空间。

Try adding an @XmlNs annotation with prefix "" for the namespace you want to appear as the default.

@XmlSchema(
    namespace = com.mycompany.foo.ATOM_NAMESPACE,
    xmlns = { 
        @XmlNs(prefix = "", namespaceURI = com.mycompany.foo.ATOM_NAMESPACE),
        @XmlNs(prefix = "foo", namespaceURI = com.mycompany.foo.NAMESPACE_FOO)
    }, 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.mycompany.web;

import javax.xml.bind.annotation.*;

注意:

@XmlSchema 注释中指定的命名空间旨在影响XML架构的生成,并且不保证在将对象模型编组为XML时使用。但是 EclipseLink JAXB(MOXy) 和最新版本的< a href =https://jaxb.java.net/ =noreferrer> JAXB参考实现将尽可能使用它们。

The namespaces specified in the @XmlSchema annotation are meant to affect the generation of the XML Schema and are not guaranteed to be used when a object model is marshalled to XML. However EclipseLink JAXB (MOXy) and recent versions of the JAXB reference implementation will use them whenever possible.

更多信息

  • http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html

这篇关于如何使用JAXB设置默认命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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