如何使用jaxb取消Marshall并从XML获取值

时间:2018-03-28 07:00:26

标签: java xml xsd jaxb unmarshalling

我的样本XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <xsd:element name="customer" type="customer-type"/>

   <xsd:complexType name="customer-type">
      <xsd:sequence>
     <xsd:element name="name" type="xsd:string"/>
     <xsd:element name="billing-address" type="address-type"/>
     <xsd:element name="shipping-address" type="address-type"/>
  </xsd:sequence>
   </xsd:complexType>


  <xsd:complexType name="address-type">
  <xsd:sequence>
     <xsd:element name="street" type="xsd:string"/>
     <xsd:element name="city" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

我的示例XML:

<?xml version="1.0" encoding="utf-8"?>
<customer>
  <name>str1234</name>
  <billing-address>
  <street>str1234</street>
  <city>str1234</city>
  </billing-address>
  <shipping-address>
   <street>str1234</street>
   <city>str1234</city>
  </shipping-address>
</customer>

我使用Eclipse中的JAXB向导生成了Pojos。它导致了三个班级。 AddressType.java,CustomerType.java和ObjectFactory.java

但是没有为元素Customer生成类。

编辑: 如何在jaxb中使用un-marshaller来检索没有Customer Pojo类的值。

1 个答案:

答案 0 :(得分:0)

您的customer元素为空,这就是未生成Customer.java的原因。 您应该向客户元素添加一些元素,如下所示:

<xsd:element name="customer">
   <xsd:complexType>
         <xsd:sequence>
            <xsd:element name = 'customerdetails' type = 'customer-type' />
               <xsd:element name = 'address' type = 'address-type' />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>

此外,在任何情况下,如果您打算在没有POJO的情况下从xml获取值,那么您可以使用sax或dom解析器解析它。

相关问题