JAXB标准方法绑定XML与Java Bean

标准化的JAXB实现Java Bean与 XML的绑定

 

XML example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wwprttxn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<price xsi:type="referencePriceElementType" type="I">
<offering>6948</offering>
<startdate>2011-04-21</startdate>
<currency>EUR</currency>
<cabletype>ANN</cabletype>
<cableid>N2Z11M026589</cableid>
<releasets>2011-04-21T19:51:17.649</releasets>
<pricevalue>0.000000</pricevalue>
<pricepointtype>FEA</pricepointtype>
<pricepointvalue>5076</pricepointvalue>
<country>AT</country>
<pricetype>XAC</pricetype>
<onshore>true</onshore>
<enddate>9999-12-31</enddate>
<pricevalueusd>0.000000</pricevalueusd>
<factor>1.000000</factor>
</price>
</wwprttxn>

P.S.

1. xs:dateTime

xs:dateTime是schema定义的日期时间型,当使用xs:dateTime()来强制转换某个字符串为日期时间型时,字符串的格式应该是:yyyy-mm-ddTHH:MM:SS
例如:2007-03-11T11:09:05, 其中的"T"为日期和时间的分割符,是必须的.

 

 

关键的步骤是定义一个合法的XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
	xmlns:this="http:///TestXML/Jaxb_test_1.xsd" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
	<xs:element name="wwprttxn">
		<xs:complexType>
		<xs:sequence>  
			<xs:element name="price" maxOccurs="unbounded" type="PriceType">
			</xs:element>
		</xs:sequence>  
	    </xs:complexType>
	 </xs:element>
	  
	<xs:complexType name="PriceType">
	<xs:sequence>	
	        <xs:element name="offering" type="xs:string"/>
	        <xs:element name="startdate" type="xs:date"/>
	        <xs:element name="currency" type="xs:string"/>
	        <xs:element name="cabletype" type="xs:string"/>
	        <xs:element name="cableid" type="xs:string"/>
	        <xs:element name="releasets" type="xs:dateTime"/>
	        <xs:element name="pricevalue" type="xs:string"/>
	        <xs:element name="pricepointtype" type="xs:string"/>
	        <xs:element name="pricepointvalue" type="xs:string"/>
	        <xs:element name="country" type="xs:string"/>
	        <xs:element name="pricetype" type="xs:string"/>
	        <xs:element name="onshore" type="xs:string"/>
	        <xs:element name="enddate" type="xs:string"/>
	        <xs:element name="pricevalueusd" type="xs:string"/>
	        <xs:element name="factor" type="xs:string"/> 
	       </xs:sequence>   
	  </xs:complexType>	  
     
 </xs:schema>

 

由这个XSD文件生成需要的JAVA Bean Skeleton:

 

最后就可以使用JAXB的标准方法将XML解析成Java Bean:

/*
 * The String Parameter is the package name which contains the ObjectFactory
*/
JAXBContext jc = JAXBContext.newInstance("test.xml");			
Unmarshaller u = jc.createUnmarshaller();			
Wwprttxn w = (Wwprttxn)u.unmarshal(new File("D:/test/53.msg"));			
List<PriceType> priceList= w.getPrice();
 

P.S.

1. JAXB2.0 里Validator类已经Deprecated.

2. JAXBContext.newInstance(String packageName), PackageName里需要包含上图中的ObjectFactory.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值