JAVA MODEL
Belos is the Java model I will use for this example:
Foo
package forum11615376;
public class Foo {
private Bar bar;
public Bar getBar() {
return bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
}
Bar
package forum11615376;
public class Bar {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
External Mapping File (oxm.xml)
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11615376">
<java-types>
<java-type name="Foo">
<xml-root-element name="FOO"/>
<java-attributes>
<xml-element java-attribute="bar" name="BAR"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
DEMO
The demo code below demonstrates how to specify the external mapping file.
Eliminate jaxb.properties
To eliminate the jaxb.properties
file (which is the standard mechanism for specifying the JAXB provider, see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html), we will use the native MOXy APIs to bootstrap the JAXBContext
.
Eliminate jaxb.index
In this example the oxm.xml
file plays the same role as jaxb.index
. Since we need to pass something in to create the JAXBContext
we will use an empty Class[]
.
package forum11615376;
import java.util.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.*;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11615376/oxm.xml");
JAXBContext jc = JAXBContextFactory.createContext(new Class[] {}, properties);
Bar bar = new Bar();
bar.setValue("Hello World");
Foo foo = new Foo();
foo.setBar(bar);
Marshaller marshaller =jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
OUTPUT
Below is the output from running the demo code. As you can see the mapping metadata was applied.
<?xml version="1.0" encoding="UTF-8"?>
<FOO>
<BAR>
<value>Hello World</value>
</BAR>
</FOO>