![](/img/trans.png)
[英]Spring web service: easy way to un-marshall a bean to XML client side?
[英]How to Un-Marshall using jaxb and get the values from XML
我的樣本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類的值。
您的客戶元素為空,這就是為什么未生成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>
另外,無論如何,如果您打算從xml中獲取值而沒有POJO,則可以使用sax或dom解析器對其進行解析。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.