JAXB入门教程

前端之家收集整理的这篇文章主要介绍了JAXB入门教程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JAXB入门教程

JAXB,全称为 Java Architecture for XML Binding,使用JAXB 注解可以实现java对象和xml相互转换,本文带你轻松入门,主要使用下面两个方法:

  1. Marshalling – 转换java对象至xml。
  2. Unmarshalling – 转换xml至java对象。

必要的软件环境:

  1. JDK 1.6
  2. JAXB 2.0

JAXB非常容易,首先在java对象上使用注解,然后使用axbMarshaller.marshal() 或 jaxbMarshaller.unmarshal()方法实现java对象/xml之间转换。

1. JAXB 依赖

如果你使用JDK1.6或以上版本,无需额外的jar包,因为其已经是JDK的内置功能。可以参考:JAXB is bundled in JDK 1.6

注:

低于1.6版本JDK,下载JAXB 从这里,把 “jaxb-api.jar”和“jaxb-impl.jar”放置项目类路径。

2. JAXB 注解

需要转换的java对象,必须使用JAXB注解。注解是很容易理解,可以参考JAXB指导详细说明。

  1. package com.mkyong.core;
  2.  
  3.  
  4. import javax.xml.bind.annotation.XmlAttribute;
  5.  
  6. import javax.xml.bind.annotation.XmlElement;
  7.  
  8. import javax.xml.bind.annotation.XmlRootElement;
  9.  
  10.  
  11. @XmlRootElement
  12.  
  13. public class Customer {
  14.  
  15.  
  16. String name;
  17.  
  18. int age;
  19.  
  20. int id;
  21.  
  22.  
  23. public String getName() {
  24.  
  25. return name;
  26.  
  27. }
  28.  
  29.  
  30. @XmlElement
  31.  
  32. public void setName(String name) {
  33.  
  34. this.name = name;
  35.  
  36. }
  37.  
  38.  
  39. public int getAge() {
  40.  
  41. return age;
  42.  
  43. }
  44.  
  45.  
  46. @XmlElement
  47.  
  48. public void setAge(int age) {
  49.  
  50. this.age = age;
  51.  
  52. }
  53.  
  54.  
  55. public int getId() {
  56.  
  57. return id;
  58.  
  59. }
  60.  
  61.  
  62. @XmlAttribute
  63.  
  64. public void setId(int id) {
  65.  
  66. this.id = id;
  67.  
  68. }
  69.  
  70.  
  71. }


3. 转换java对象至XML

JAXB marshalling 示例,转换customer 对象至xml文件jaxbMarshaller.marshal() 有许多重载方法,找到你需要的方法实现正确输出。

  1. package com.mkyong.core;
  2.  
  3.  
  4. import java.io.File;
  5.  
  6. import javax.xml.bind.JAXBContext;
  7.  
  8. import javax.xml.bind.JAXBException;
  9.  
  10. import javax.xml.bind.Marshaller;
  11.  
  12.  
  13. public class JAXBExample {
  14.  
  15. public static void main(String[] args) {
  16.  
  17.  
  18. Customer customer = new Customer();
  19.  
  20. customer.setId(100);
  21.  
  22. customer.setName("mkyong");
  23.  
  24. customer.setAge(29);
  25.  
  26.  
  27. try {
  28.  
  29.  
  30. File file = new File("C:\\file.xml");
  31.  
  32. JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  33.  
  34. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  35.  
  36.  
  37. // output pretty printed
  38.  
  39. jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  40.  
  41.  
  42. jaxbMarshaller.marshal(customer,file);
  43.  
  44. jaxbMarshaller.marshal(customer,System.out);
  45.  
  46.  
  47. } catch (JAXBException e) {
  48.  
  49. e.printStackTrace();
  50.  
  51. }
  52.  
  53.  
  54. }
  55.  
  56. }


输出结果:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2.  
  3. <customer id="100">
  4.  
  5. <age>29</age>
  6.  
  7. <name>mkyong</name>
  8.  
  9. </customer>


4. 转换XML 至java对象

JAXB unmarshalling 示例,转换 XML 文件内容至customer 对象. jaxbMarshaller.unmarshal() 有许多重载方法,找到你需要的方法实现正确输出

  1. package com.mkyong.core;
  2.  
  3.  
  4. import java.io.File;
  5.  
  6. import javax.xml.bind.JAXBContext;
  7.  
  8. import javax.xml.bind.JAXBException;
  9.  
  10. import javax.xml.bind.Unmarshaller;
  11.  
  12.  
  13. public class JAXBExample {
  14.  
  15. public static void main(String[] args) {
  16.  
  17.  
  18. try {
  19.  
  20.  
  21. File file = new File("C:\\file.xml");
  22.  
  23. JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  24.  
  25.  
  26. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  27.  
  28. Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
  29.  
  30. System.out.println(customer);
  31.  
  32.  
  33. } catch (JAXBException e) {
  34.  
  35. e.printStackTrace();
  36.  
  37. }
  38.  
  39.  
  40. }
  41.  
  42. }


输出结果:

  1. [===]Customernamemkyong,age29,id100

参考学习内容

  1. JAXB Official Website
  2. J2EE 5 JAXB tutorial

猜你在找的XML相关文章