JAXB,MOXy;如果我有@XmlPath(“。”)自映射,我可以使用@XmlPaths和@XmlElements来建模选择吗?

时间:2015-11-13 14:19:17

标签: java xml jaxb moxy

假设我的模式具有内部选择的类型;

<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>

我可能希望通过包装java类来表示A,B,C的序列,以区别于P或Q.我使用MOXy的'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;
     }
}

请参阅此前question and answer

但是假设我更喜欢以一种更自然的方式来表示模式选择;

@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将负责

    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>

但是当我解组它时RootType2是空的;关于重新编组,我得到了;

<?xml version="1.0" encoding="UTF-8"?>
<TopLevel2 xmlns="http://www.example.org/Schema1"/>

好吧,也许我需要通过告诉它如果A或B或C在TopLevel中应该映射到InnerSequence来触及MOXy,所以我尝试了这个XPath(检查它是否在独立的java中工作); < / p>

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;

...

然后我不能马歇尔,获得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和@XmlElements结合使用?

Eclipselink MOXy forum

上的同样问题

0 个答案:

没有答案