Java操作XML的JAXB工具

來源:互聯網
上載者:User

標籤:mod   ring   hal   return   catch   base64   class   imp   div   

在java中操作XML的工作中中,比較方便的工具是JAXB(Java Architecture for XML Binding)。

利用這個工具很方便產生XML的tag和Java類的對應關係。參照網上的資料,簡單說明一下java操作xml的一些東西。

1.先定義一個XML Schema檔案。比如:

 

[html] view plain copy  print?
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <xs:schema version="1.0"  
  3.      xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  4.    
  5.   <xs:element name="person">  
  6.     <xs:complexType>  
  7.       <xs:attribute name="name" type="xs:string"/>  
  8.       <xs:attribute name="age" type="xs:int"/>  
  9.     </xs:complexType>  
  10.   </xs:element>  
  11. </xs:schema>  


相關XML Schema的定義規則可以參考http://www.w3.org/TR/xmlschema-2/

 

 

2.產生java類。

在命令列中執行

C:\jaxb>xjc person.xsd

 

這樣就產生了Persion類。

[java] view plain copy  print?
  1. package com.test.xml  
  2.   
  3. public class Person {  
  4.   
  5.     protected String name;  
  6.     protected Integer age;  
  7.    
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.    
  12.     public void setName(String value) {  
  13.         this.name = value;  
  14.     }  
  15.    
  16.     public Integer getAge() {  
  17.         return age;  
  18.     }  
  19.    
  20.     public void setAge(Integer value) {  
  21.         this.age = value;  
  22.     }  
  23. }  



 

 

tag的類型和java的類型的對應關係。

 

XML Schema Type Java Data Type
xsd:string java.lang.String
xsd:integer java.math.BigInteger
xsd:int int
xsd.long long
xsd:short short
xsd:decimal java.math.BigDecimal
xsd:float float
xsd:double double
xsd:boolean boolean
xsd:byte byte
xsd:QName javax.xml.namespace.QName
xsd:dateTime javax.xml.datatype.XMLGregorianCalendar
xsd:base64Binary byte[]
xsd:hexBinary byte[]
xsd:unsignedInt long
xsd:unsignedShort int
xsd:unsignedByte short
xsd:time javax.xml.datatype.XMLGregorianCalendar
xsd:date javax.xml.datatype.XMLGregorianCalendar
xsd:g javax.xml.datatype.XMLGregorianCalendar
xsd:anySimpleType java.lang.Object
xsd:anySimpleType java.lang.String
xsd:duration javax.xml.datatype.Duration
xsd:NOTATION javax.xml.namespace.QName

 

 

3. 讀取xml的內容到java對象。

java讀取xml的內容的例子。

 

[java] view plain copy  print?
  1. public class UnmarshallerUtil {  
  2.       
  3.    public UnmarshallerUtil () {  
  4.         try {  
  5.             // 1. 產生JAXBContex對象  
  6.             JAXBContext context = JAXBContext.newInstance("com.test.xml");  
  7.                   
  8.             // 2. 產生Unmarsaller對象  
  9.             Unmarshaller unmarshaller = context.createUnmarshaller();  
  10.    
  11.             File file = new File("artists.xml");  
  12.    
  13.             // 3. Unmarsaller  
  14.             Object obj = unmarshaller.unmarshal(file);  
  15.             Persons artists = (Persons)obj;  
  16.   
  17.             //其他處理  
  18.          
  19.         } catch (JAXBException ex) {  
  20.   
  21.         }  
  22.     }  
  23.   
  24.     public static void main(String[] args) {  
  25.         new UnmarshallerUtil();  
  26.     }  
  27. }  



 

Java操作XML的JAXB工具

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.