I\' have simple question about JAXB. Here is the sample code:
//setter that has input JAXBElement
b.setBIC(JAXBElement value);
You can create an instance of JAXBElement
directly or if you generated your Java model from an XML schema use a convience method on the generated ObjectFactory
class.
package org.example.schema;
import javax.xml.bind.*;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("org.example.schema");
Root root = new Root();
QName fooQName = new QName("http://www.example.org/schema", "foo");
JAXBElement fooValue = new JAXBElement(fooQName, String.class, "FOO");
root.setFoo(fooValue);
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement barValue = objectFactory.createRootBar("BAR");
root.setBar(barValue);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
schema.xsd
The above demo code is based on a Java model generated from the following XML schema. The reason you get a JAXBElement
property in the first place is when you have an element that is both nillable="true"
and minOccurs="0"
.
Root
The following class was generated from schema.xsd
and contains properties like the one in your question.
package org.example.schema;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"foo","bar"})
@XmlRootElement(name = "root")
public class Root {
@XmlElementRef(name = "foo", namespace = "http://www.example.org/schema", type = JAXBElement.class)
protected JAXBElement foo;
@XmlElementRef(name = "bar", namespace = "http://www.example.org/schema", type = JAXBElement.class)
protected JAXBElement bar;
public JAXBElement getFoo() {
return foo;
}
public void setFoo(JAXBElement value) {
this.foo = value;
}
public JAXBElement getBar() {
return bar;
}
public void setBar(JAXBElement value) {
this.bar = value;
}
}
ObjectFactory
Below is the generated ObjectFactory
class that contains convenience methods for creating the instances of JAXBElement
.
package org.example.schema;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _RootFoo_QNAME = new QName("http://www.example.org/schema", "foo");
private final static QName _RootBar_QNAME = new QName("http://www.example.org/schema", "bar");
public Root createRoot() {
return new Root();
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "foo", scope = Root.class)
public JAXBElement createRootFoo(String value) {
return new JAXBElement(_RootFoo_QNAME, String.class, Root.class, value);
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "bar", scope = Root.class)
public JAXBElement createRootBar(String value) {
return new JAXBElement(_RootBar_QNAME, String.class, Root.class, value);
}
}