Suppose I have a schema with a type which has an inner choice;
<complexType name="RootType">
<sequence minOccurs="1" maxOccurs="1">
<choice>
<sequence minOccurs="1" maxOccurs="1">
<element name="A" type="tns:A"></element>
<element name="B" type="tns:B"></element>
<element name="C" type="tns:C"></element>
</sequence>
<element name="P" type="tns:P" minOccurs="1" maxOccurs="1"></element>
<element name="Q" type="tns:Q" minOccurs="1" maxOccurs="1"></element>
</choice>
</sequence>
</complexType>
I might want to represent the sequence of A, B, C by a wrapping java class to distinguish it from the P or Q. I represent structure which as no name using MOXy's 'self mappings';
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "RootType", propOrder = {
"seq1",
"p",
"q"
})
@XmlRootElement(name="TopLevel")
public class RootType {
@XmlPath(".")
private InnerSequence seq1;
@XmlElement(name = "P")
private P p;
@XmlElement(name = "Q")
private Q q;
public static class InnerSequence
{
@XmlElement(name = "A")
public A a;
@XmlElement(name = "B")
public B b;
@XmlElement(name = "C")
public C c;
}
}
See this previous question and answer
But suppose I prefered to represent that in a way more natural to a schema choice;
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "RootType2", propOrder = {
"choice1"
})
@XmlRootElement(name="TopLevel2")
public class RootType2 {
@XmlPaths({
@XmlPath("."),
@XmlPath("P"),
@XmlPath("Q"),
})
@XmlElements({
@XmlElement(type=InnerSequence.class),
@XmlElement(name = "P", type=P.class),
@XmlElement(name = "Q", type=Q.class),
})
public Object choice1;
public static class InnerSequence
{
@XmlElement(name = "A")
public A a;
@XmlElement(name = "B")
public B b;
@XmlElement(name = "C")
public C c;
}
}
Moxy will marshall
RootType2 root1 = new RootType2();
InnerSequence inner1 = new InnerSequence();
root1.choice1 = inner1;
inner1.a = new A();
inner1.b = new B();
inner1.c = new C();
inner1.a.setOne("Hello One A");
inner1.b.setOne("Hello One B");
inner1.c.setOne("Hello One C");
as expected to
<?xml version="1.0" encoding="UTF-8"?>
<TopLevel2 xmlns="http://www.example.org/Schema1">
<A>
<one>Hello One A</one>
</A>
<B>
<one>Hello One B</one>
</B>
<C>
<one>Hello One C</one>
</C>
</TopLevel2>
But when I unmarshall it RootType2 is empty; on remarshalling I get;
<?xml version="1.0" encoding="UTF-8"?>
<TopLevel2 xmlns="http://www.example.org/Schema1"/>
Okay, so perhaps I need to kick MOXy into life by telling it somehow that InnerSequence should be mapped if A or B or C are in TopLevel, so I try this XPath (having checked that it works in standalone java);
public class RootType2 {
@XmlPaths({
@XmlPath("self::node()[A|B|C]/."),
@XmlPath("P"),
@XmlPath("Q"),
})
@XmlElements({
@XmlElement(type=InnerSequence.class),
@XmlElement(name = "P", type=P.class),
@XmlElement(name = "Q", type=Q.class),
})
public Object choice1;
...
I then cannot Marshall, getting an NPE:
javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.MarshalException
- with linked exception:
[java.lang.NullPointerException]]
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:508)
at uk.co.his.test.complexSequenceAndChoice.TestSchema3.testNACT1_jaxb(TestSchema3.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[java.lang.NullPointerException]
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:487)
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:501)
... 25 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.oxm.record.OutputStreamRecord.outputStreamWrite(OutputStreamRecord.java:581)
at org.eclipse.persistence.oxm.record.OutputStreamRecord.outputStreamWrite(OutputStreamRecord.java:568)
at org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord.openStartElement(FormattedOutputStreamRecord.java:120)
at org.eclipse.persistence.oxm.record.MarshalRecord.openStartGroupingElements(MarshalRecord.java:482)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:168)
at org.eclipse.persistence.internal.oxm.XMLChoiceObjectMappingNodeValue.marshalSingleValue(XMLChoiceObjectMappingNodeValue.java:155)
at org.eclipse.persistence.internal.oxm.XMLChoiceObjectMappingNodeValue.marshal(XMLChoiceObjectMappingNodeValue.java:106)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:443)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:424)
at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:243)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:766)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshalStreamOrWriter(XMLMarshaller.java:1147)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:934)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:877)
at org.eclipse.persistence.
Is there any way to use a Self Mapping in conjunction with an @XmlPaths and @XmlElements?
[Updated on: Fri, 13 November 2015 15:11]
Report message to a moderator