JAXB Hello World

[转自] http://blog.csdn.net/tounaobun/article/details/8763799


JAXB是Java Architecture for XML Binding的缩写。使用JAXB注解将Java对象转换成XML文件。在这篇教程中,我们将会展示如何使用JAXB来做以下事情:

  1.     Marshalling       - 将Java对象转换成XML文件。
  2.     Unmarshalling - 将XML内容转换成Java对象。

    本文使用到的相关技术:

  1.     JDK 1.6
  2.     JAXB 2.0

    使用JAXB很简单。只需用JAXB注解标注对象,然后使用jaxbMarshaller.marshal() 或者 jaxbMarshaller.unmarshal() 来做 XML/Object 的转换工作。

  1.JAXB 依赖 

    如果你使用的时JDK1.6或以上版本,你不需要添加额外的类库,因为JAXB被绑定在JDK1.6中。

    注释:

             如果你使用的时JDK < 1.6,你需要将下载的"jaxb-api.jar"和"jaxb-impl.jar"包添加到你的项目CLASSPATH中。

     

  2.JAXB 注解(Annotation)

    如果一个对象需要被转换成XML文件,或者从XML文件中生成,该对象需要用JAXB注解来标注。这些注解光凭名字就知道是什么意思了。具体可参考官网:jaxb guide

   
  1. package com.jaxb.core;  
  2.    
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4. import javax.xml.bind.annotation.XmlElement;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6.    
  7. @XmlRootElement  
  8. public class Customer {  
  9.    
  10.     String name;  
  11.     int age;  
  12.     int id;  
  13.    
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.    
  18.     @XmlElement  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.    
  23.     public int getAge() {  
  24.         return age;  
  25.     }  
  26.    
  27.     @XmlElement  
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.    
  32.     public int getId() {  
  33.         return id;  
  34.     }  
  35.    
  36.     @XmlAttribute  
  37.     public void setId(int id) {  
  38.         this.id = id;  
  39.     }  
  40.    
  41. }  


    3.对象转换成XML

        JAXB marshalling例子,将customer对象转换成XML文件。jaxbMarshaller.marshal()包含了许多重载方法,哪个输出符合你的要求就选择哪个方法。

  1. package com.jaxb.core;  
  2.    
  3. import java.io.File;  
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.JAXBException;  
  6. import javax.xml.bind.Marshaller;  
  7.    
  8. public class JAXBExample {  
  9.     public static void main(String[] args) {  
  10.    
  11.       Customer customer = new Customer();  
  12.       customer.setId(100);  
  13.       customer.setName("benson");  
  14.       customer.setAge(23);  
  15.    
  16.       try {  
  17.    
  18.         File file = new File("C:\file.xml");  
  19.         JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
  20.         Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
  21.    
  22.         // output pretty printed  
  23.         jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  24.    
  25.         jaxbMarshaller.marshal(customer, file);  
  26.         jaxbMarshaller.marshal(customer, System.out);  
  27.    
  28.           } catch (JAXBException e) {  
  29.         e.printStackTrace();  
  30.           }  
  31.    
  32.     }  
  33. }  

    输出:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer id="100">  
  3.     <age>23</age>  
  4.     <name>benson</name>  
  5. </customer>  


 

    4.XML转换成对象:

    JAXB unmarshalling例子,将XML文件内容转换成customer对象。jaxbMarshaller.unmarshal()包含了许多重载方法,哪个适合你的输出,你就选择哪个方法。

  1. package com.jaxb.core;  
  2.    
  3. import java.io.File;  
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.JAXBException;  
  6. import javax.xml.bind.Unmarshaller;  
  7.    
  8. public class JAXBExample {  
  9.     public static void main(String[] args) {  
  10.    
  11.      try {  
  12.    
  13.         File file = new File("C:\file.xml");  
  14.         JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
  15.    
  16.         Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
  17.         Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);  
  18.         System.out.println(customer);  
  19.    
  20.       } catch (JAXBException e) {  
  21.         e.printStackTrace();  
  22.       }  
  23.    
  24.     }  
  25. }  


    输出:

  1. Customer [name=benson, age=23, id=100]  


原文地址:https://www.cnblogs.com/pekkle/p/6568718.html