|
Re: MOXy: xml-mapping-metadata-complete problem [message #789940 is a reply to message #789127] |
Fri, 03 February 2012 15:49 |
|
The MOXy external mapping document applies at the package level so you will need to have one per package. Below is an example.
feb3.foo.Foo
The following class represents the annotated super class in one package:
package feb3.foo;
import javax.xml.bind.annotation.XmlAttribute;
public class Foo {
private String foo;
@XmlAttribute
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
feb3.bar.Bar
And this class represents the annotated subclass in another package.
package feb3.bar;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import feb3.foo.Foo;
@XmlRootElement
public class Bar extends Foo {
private String bar;
@XmlAttribute
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
feb3/foo/oxm.xml
We need one external mapping document to override the metadata in the feb3.foo package.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="feb3.foo" xml-mapping-metadata-complete="true"/>
feb3/bar/oxm.xml
And we need another external mapping document to override the metadata in the feb3.bar package.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="feb3.bar" xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Bar">
<xml-root-element/>
</java-type>
</java-types>
</xml-bindings>
feb3.demo.Demo
The following demo code demonstrates how to bootstrap a JAXBContext from multiple external mapping documents.
package feb3.demo;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import feb3.bar.Bar;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
List<String> oxm = new ArrayList<String>(2);
oxm.add("feb3/foo/oxm.xml");
oxm.add("feb3/bar/oxm.xml");
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Bar.class}, properties);
Bar bar = new Bar();
bar.setFoo("FOO");
bar.setBar("BAR");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bar, System.out);
}
}
Output
The following is the output from running the demo code. Note how the @XmlAttribute annotations on both the Foo and Bar classes have been ignored:
<?xml version="1.0" encoding="UTF-8"?>
<bar>
<foo>FOO</foo>
<bar>BAR</bar>
</bar>
Uses of xml-mapping-metadata-complete="true"
This flag is useful when you want to apply a second mapping to your object model that is completely different from the current mapping. Below is an example where this is used to map one object model to both the Google and Yahoo weather APIs:
http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
|
|
|
Re: MOXy: xml-mapping-metadata-complete problem [message #789942 is a reply to message #789127] |
Fri, 03 February 2012 15:49 |
|
The MOXy external mapping document applies at the package level so you will need to have one per package. Below is an example.
feb3.foo.Foo
The following class represents the annotated super class in one package:
package feb3.foo;
import javax.xml.bind.annotation.XmlAttribute;
public class Foo {
private String foo;
@XmlAttribute
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
feb3.bar.Bar
And this class represents the annotated subclass in another package.
package feb3.bar;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import feb3.foo.Foo;
@XmlRootElement
public class Bar extends Foo {
private String bar;
@XmlAttribute
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
feb3/foo/oxm.xml
We need one external mapping document to override the metadata in the feb3.foo package.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="feb3.foo" xml-mapping-metadata-complete="true"/>
feb3/bar/oxm.xml
And we need another external mapping document to override the metadata in the feb3.bar package.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="feb3.bar" xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Bar">
<xml-root-element/>
</java-type>
</java-types>
</xml-bindings>
feb3.demo.Demo
The following demo code demonstrates how to bootstrap a JAXBContext from multiple external mapping documents.
package feb3.demo;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import feb3.bar.Bar;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
List<String> oxm = new ArrayList<String>(2);
oxm.add("feb3/foo/oxm.xml");
oxm.add("feb3/bar/oxm.xml");
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Bar.class}, properties);
Bar bar = new Bar();
bar.setFoo("FOO");
bar.setBar("BAR");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bar, System.out);
}
}
Output
The following is the output from running the demo code. Note how the @XmlAttribute annotations on both the Foo and Bar classes have been ignored:
<?xml version="1.0" encoding="UTF-8"?>
<bar>
<foo>FOO</foo>
<bar>BAR</bar>
</bar>
Uses of xml-mapping-metadata-complete="true"
This flag is useful when you want to apply a second mapping to your object model that is completely different from the current mapping. Below is an example where this is used to map one object model to both the Google and Yahoo weather APIs:
http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03424 seconds