How do I set the elementFormDefault of an element in JAXB based on a single class instead of having it set for the whole package?

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

+3


source to share


1 answer


TL; DR

When elementFormDefault=XmlNsForm.QUALIFIED

set to @XmlSchema

, you can override the namespace for class properties by annotating it with @XmlType(namespace="ANOTHER_NAMESPACE")

. If you want to override the namespace for the root element, you can do @XmlRootElement(namespace="DIFFERENT_NAMESPACE)

.

Additional Information


JAVA MODEL

Bar

You just need to set the namespace in the annotation @XmlType

for the class Bar

.

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

This will override the namespace specified in @XmlSchema

the package level annotation .

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

import javax.xml.bind.annotation.*;

      

Foo

This object is the root of your domain model.



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 SCHEMAS

Below are the XML schemas you provided with http://jsfiddle.net/supertonsky/Phck5/ .

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

<?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>

      

DEMO CODE

The following demo code will instantiate a domain object and output it to XML. The XML output will be checked during a marshal operation against your proposed XML schemas.

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);
    }

}

      

Output

Then you will get the following output. Note that the element name

that matches the property name

on Bar

is namespace FOO

-qualified, and all other elements are namespace-qualified FOO2

.

<?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>

      

+5


source







All Articles