Java JAXB,MOXy;如果我有一个@XmlPath(";)自映射,我可以使用@XmlPath和@xmlmelements为选择建模吗?
假设我有一个模式,其类型具有内部选择Java JAXB,MOXy;如果我有一个@XmlPath(";)自映射,我可以使用@XmlPath和@xmlmelements为选择建模吗?,java,xml,jaxb,moxy,Java,Xml,Jaxb,Moxy,假设我有一个模式,其类型具有内部选择 <complexType name="RootType"> <sequence minOccurs="1" maxOccurs="1"> <choice> <sequence minOccurs="1" maxOccurs="1"> <element name="A" type="tns:A"&
<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>
见前一页
但假设我更愿意用一种比模式选择更自然的方式来表示这一点
@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;
}
}
莫西·威尔·马歇尔
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");
果然
<?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>
然后我就不能马歇尔,获得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.
有没有办法将自映射与@XmlPaths和@xmlements结合使用
同样的问题嗨,我知道它很老了,但就连我最近也陷入了这个问题。您是否知道解决问题的解决方案或变通方法?以下是我的观察:我试图调试
Moxy
代码,但仍然没有找到解决方案。但我知道问题的发生是由于注释@XmlPath(“.”
)。正如我们给出的(“)
一样,所有字段都将解组
到映射中
。我想这是Moxy
的预期工作流程,因此不是一个bug。我尝试了很多变通办法,但没有成功。我在这里的帖子:
<?xml version="1.0" encoding="UTF-8"?>
<TopLevel2 xmlns="http://www.example.org/Schema1"/>
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;
...
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.