如何在单个类的基础上设置JAXB中的Element的elementFormDefault,而不是为整个包设置它? [英] How do I set the elementFormDefault of an Element in JAXB on a single class basis instead of having it set for the entire package?

查看:92
本文介绍了如何在单个类的基础上设置JAXB中的Element的elementFormDefault,而不是为整个包设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用@XmlSchema来执行此操作,但问题是我们在同一包中有一个类,该类需要其名称空间与package-info.java上定义的名称空间不同.因此,我们改为在@XmlRootElement中声明了名称空间(当然,在仍然保留@XmlSchema的情况下).但是这样做将不允许我们设置类的elementFormDefault.不能将类移到另一个包.基本上,我只想覆盖此特定类的名称空间.

I know I could use @XmlSchema to do this but the problem is we have a class in the same package that needs its namespace to be different from what's defined on package-info.java. So we declared the namespace in @XmlRootElement instead (of course while still having @XmlSchema in place). But doing this will not allow us to set the class's elementFormDefault. Moving the class to a different package is not an option. Basically, I just want to override the namespace for this particular class.

推荐答案

TL; DR

@XmlSchema上设置了elementFormDefault=XmlNsForm.QUALIFIED时,可以通过用@XmlType(namespace="ANOTHER_NAMESPACE")进行注释来覆盖类属性的名称空间.如果要覆盖根元素的名称空间,可以执行@XmlRootElement(namespace="DIFFERENT_NAMESPACE).

When elementFormDefault=XmlNsForm.QUALIFIED is set on @XmlSchema, you can override then namespace for the properties of a class by annotating it with @XmlType(namespace="ANOTHER_NAMESPACE"). If you want to override the namespace for a root element you can do @XmlRootElement(namespace="DIFFERENT_NAMESPACE).

更多信息

For More Information

JAVA模型

酒吧

Bar

您只需要在Bar类的@XmlType注释上设置名称空间.

You just need to set the namespace on the @XmlType annotation for the Bar class.

package forum14579814;

import javax.xml.bind.annotation.XmlType;

@XmlType(namespace="FOO")
public class Bar {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

包装信息

package-info

这将覆盖在包级别@XmlSchema批注中指定的名称空间.

This will override the namespace that you specified in package level @XmlSchema annotation.

@XmlSchema(namespace="FOO2", elementFormDefault=XmlNsForm.QUALIFIED)
package forum14579814;

import javax.xml.bind.annotation.*;

Foo

Foo

该对象是域模型的根.

package forum14579814;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Foo {

    private Bar bar;

    public Bar getBar() {
        return bar;
    }

    public void setBar(Bar bar) {
        this.bar = bar;
    }

}

XML模式

下面是您通过 http://jsfiddle.net/supertonsky/Phck5/.

Foo.xsd

Foo.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="FOO2"
    xmlns:tns="FOO2" 
    xmlns:tns2="FOO"
    elementFormDefault="qualified">
    <import namespace="FOO" schemaLocation="BAR.xsd"></import>
    <element name="foo" type="tns:Foo"></element>
    <complexType name="Foo">
        <sequence>
            <element name="bar" type="tns2:Bar"></element>
        </sequence>
    </complexType>
</schema>

Bar.xsd

Bar.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="FOO"
    xmlns:tns="FOO" 
    elementFormDefault="qualified">
    <complexType name="Bar">
        <sequence>
            <element name="name" type="string" maxOccurs="1" nillable="true"></element>
        </sequence>
    </complexType>
</schema>

演示代码

以下演示代码将创建域对象的实例并将其输出到XML.将在编组操作期间根据您提供的XML模式验证XML输出.

The following demo code will create an instance of the domain object and output it to XML. The XML output will be validated during the marshal operation against the XML schemas that you provided.

package forum14579814;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.validation.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Bar bar = new Bar();
        bar.setName("BAR");

        Foo foo = new Foo();
        foo.setBar(bar);

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new File("src/forum14579814/Foo.xsd"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setSchema(schema);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

输出

然后您将获得以下输出.请注意,与Barname属性相对应的name元素如何用FOO命名空间限定,而所有其他元素都用FOO2命名空间限定.

Then you would get the following output. Note how the name element that corresponds to the name property on Bar is qualified with the FOO namespace while all the other elements are qualified with the FOO2 namespace.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:foo xmlns="FOO" xmlns:ns2="FOO2">
    <ns2:bar>
        <name>BAR</name>
    </ns2:bar>
</ns2:foo>

这篇关于如何在单个类的基础上设置JAXB中的Element的elementFormDefault,而不是为整个包设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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