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

查看:61
本文介绍了如何使用 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) 和最新版本的 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.

了解更多信息

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

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