JAXB unmarshall:ElementNSImpl来自哪里?

时间:2015-11-24 23:49:15

标签: java xml xsd jaxb

当我解组某个对象时,我不会找回该对象的类型,但类型ElementNSImpl似乎包含了我的数据但是为什么JAXB没有将其解组为我的JAXB类?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Asset", propOrder = {
    "platformContentOrFeatureContentOrAirspace",
    ...
})
@XmlSeeAlso({
    ...
})
public abstract class Asset {
    @XmlElements({
        @XmlElement(name = "PlatformContent", type = AnyPlatform.class),
        @XmlElement(name = "FeatureContent", type = AnyFeature.class),
        @XmlElement(name = "Airspace", type = Airspace.class)
    })
    protected Object platformContentOrFeatureContentOrAirspace;

    ...

    public Object getPlatformContentorFeatureContentorAirspace(){
        return platformContentOrFeatureContentOrAirspace;
    }
    ...
}

这是一个架构片段:

<complexType name="Asset">
    <complexContent>
      <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
        <sequence>
          <choice minOccurs="0">
            <element name="PlatformContent" type="temp:TargetNamespaceURI}AnyPlatform"/>
            <element name="FeatureContent" type="{temp:TargetNamespaceURI}AnyFeature"/>
            <element ref="{temp:TargetNamespaceURI}Airspace"/>
          </choice>
        </sequence>
      </restriction>
    </complexContent>
  </complexType>

更新 在这种情况下,我试图解组包含PlatformContent标记的XML。如果我取出@XmlTransient注释,我会得到一个例外,说它不能实例化AnyPlatform。我对JAXB很陌生,但我意识到这可能是一个错误的步骤,并且在我离开@XmlTransient之后踩过我的问题。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AnyPlatform", propOrder = {
    "fooOrBarOrBaz"
})
@XmlTransient   //This may be needed to parse correctly.
@XmlSeeAlso({
     AnyPlatformDerivedType1.class,
     AnyPlatformDerivedType2.class
})
public abstract class AnyPlatform {

    @XmlElements({
        @XmlElement(name = "Foo", type = Foo.class),
        @XmlElement(name = "Bar", type = Bar.class),
        @XmlElement(name = "Baz", type = Baz.class),
    })
    protected PlatformType fooOrBarOrBaz;

    public PlatformType getFooOrBarOrBaz() {
        return fooOrBarOrBaz;
    }
}

这是架构片段

<xs:complexType name="AnyPlatform" abstract="true">
    <xs:choice>
        <xs:element ref="Foo"/>
        <xs:element ref="Bar"/>
        <xs:element ref="Baz"/>
    </xs:choice>
</xs:complexType>

当@XmlTransient被取出时,我得到了这个例外:

javax.xml.bind.UnmarshalException: Unable to create an instance of AnyPlatform
 - with linked exception:
[java.lang.InstantiationException]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:690)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:171)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader.startElement(XsiTypeLoader.java:65)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:559)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2787)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:125)
    at App.parse(App.java:66)
    at MainWindow$1.actionPerformed(MainWindow.java:57)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:118)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:270)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:684)
    ... 60 more

更新2  这是有问题的XML:

<p:AssetList>
  <p:Asset>
    <p:PlatformContent>
      <p:Foo>
        <!-- Foo's Data -->
      </p:Foo>
    </p:PlatformContent>
  </p:Asset>
</p:AssetList>

UPDATE3   为了增加更多的清晰度,AnyPlatform是两个派生类的基类。我们将它们称为AnyPlatformDerivedClass1和AnyPlatformDerivedClass2。这是他们的样子:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AnyPlatformDerivedType1")
public class AnyPlatformDerivedType1
    extends AnyPlatform
{
// Yes. These really are empty according to the schema.

}

根据架构,它们是空白的。所以这基本上只是具有不同XML标记名称的相同内容。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AnyPlatformDerivedType2")
public class AnyPlatformDerivedType2
    extends AnyPlatform
{
// Yes. These really are empty according to the schema.

}

2 个答案:

答案 0 :(得分:1)

如果您使用xsi:type作为鉴别器并且具体实现在JAXBContext中(直接添加或通过XmlSeeAlso引用),它将充分发挥作用。这是一个例子:

class QuickTest {

    @XmlRootElement
    public static class Container {
        @XmlElement
        public Base base;
    }

    public static abstract class Base {

    }

    @XmlType(name="one")
    public static class ChildOne extends Base {

    }

    @XmlType(name="two")
    public static class ChildTwo extends Base {

    }

    private static final String MESSAGE = "<container xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><base xsi:type=\"one\"/></container>";


    public static void main(String [] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance(Container.class, Base.class, ChildOne.class, ChildTwo.class);
        Container con = (Container) context.createUnmarshaller().unmarshal(new StringReader(MESSAGE));
        System.out.println(con.base.getClass());
        //Prints QuickTest$ChildOne
    }
}

答案 1 :(得分:0)

我最终从AnyPlatform中删除了abstract关键字。一切都按预期工作,现在我不太可能在将来需要它。