Jaxb ClassCastException,带有XML和自定义绑定中的匿名内部类型

时间:2018-06-29 20:43:59

标签: xml binding xsd jaxb unmarshalling

我有以下xsd文件:     

<xs:element name="shiporder">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="item" minOccurs="0" nillable="true" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="title" type="xs:string"/>
                        <xs:element name="item" minOccurs="0" nillable="true" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="note" type="xs:string" minOccurs="0"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="item">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="item" minOccurs="0" nillable="true" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="description"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

和以下绑定。xjb

<jxb:bindings version="1.0"
          xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">    <jxb:globalBindings localScoping="toplevel">
    <xjc:simple/>
</jxb:globalBindings>
<jxb:bindings schemaLocation="model.xsd">
    <jxb:schemaBindings>
        <jxb:package name="test"/>
    </jxb:schemaBindings>

    <jxb:bindings node="/xs:schema/xs:element[@name='shiporder']">
        <jxb:class name="Shiporderrrr"/>
    </jxb:bindings>

    

    <jxb:bindings node="/xs:schema/xs:element[@name='shiporder']/xs:complexType/xs:sequence/xs:element[@name='item']/xs:complexType/xs:sequence/xs:element[@name='item']">
        <jxb:class name="ShiporderItemItem"/>
    </jxb:bindings>
    <jxb:bindings node="/xs:schema/xs:element[@name='item']">
        <jxb:class name="Item"/>
    </jxb:bindings>
    <jxb:bindings node="/xs:schema/xs:element[@name='item']/xs:complexType/xs:sequence/xs:element[@name='item']">
        <jxb:class name="ItemItem"/>
    </jxb:bindings>
</jxb:bindings>

我也有以下xml文件:

<shiporder>
  <item>
    <title>string1</title>
     <item>
      <note>string1</note>
     </item>
    </item>
</shiporder>

当我使用jaxb生成的类解组时:

import test.Shiporderrrr;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.nio.file.Paths;

public class JaxbTest {
    public static void main(String[] args) {
       try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Shiporderrrr.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Shiporderrrr shiporder = (Shiporderrrr)unmarshaller.unmarshal(Paths.get("src/main/resources/model1.xml").toFile());
        System.out.println(shiporder.getItems().get(0).getValue().getTitle());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

我收到以下异常:

  

java.lang.ClassCastException:无法将test.ShiporderItem $ ShiporderItemItem转换为test.ShiporderItem
      在JaxbTest.main(JaxbTest.java:16)

您能帮我在这里找到我做错的吗?
我认为这里的一切都应该正常工作。
我应该以其他方式重写绑定吗?

1 个答案:

答案 0 :(得分:0)

我发现问题出在最后,缺少绑定 / xs:complexType 。嵌套类型的绑定应如下所示:

<jxb:bindings node="/xs:schema/xs:element[@name='item']/xs:complexType/xs:sequence/xs:element[@name='item']/xs:complexType">
    <jxb:class name="ItemItem"/>
</jxb:bindings>

,然后就没有ClassCastException。