JAXB--简单应用(一)

感恩原创:http://suo.iteye.com/blog/1233458

一、简介

1、概念是什么:(Java Architecture for XML Binding) 是一个业界的标准,即是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。有多种实现。



2、JAXB中有什么:包含“xjc”工具和一个“schemagen”工具。 

“xjc”工具可以用来将XML模式或其他类型模式文件(Java 1.6试验性地支持RELAX NG,DTD以及WSDL)转换为Java类。Java类使用javax.xml.bind.annotation包下的Java 标注,例如@XmlRootElement和@XmlElement。XML列表序列表示为java.util.List类型的属性, 通过JAXBContext可以创建Marshallers(将Java对象转换成XML)和Unmarshallers(将XML解析为Java对象)。

另外的“schemagen”工具,能够执行“xjc”的反向操作,通过一组标注的Java类创建一个XML模式。 

二、依赖

JDK5以下开发需要的jar包:activation.jar、jaxb-api.jar、 jaxb-impl.jar、 jsr173-api.jar;

如果是基于JDK6以上版本已经集成JAXB2的JAR,在目录{JDK_HOME}/jre/lib/rt.jar中。 

三、简单应用

第一步:创建要转化的java对象,该对象需要使用相关注解注释各字段(标注在get方法上)。
package step1;  
import javax.xml.bind.annotation.XmlAttribute;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;  

@XmlRootElement  
public class Customer {  
    String name;  
    int age;  
    int id;  

    @XmlElement  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  

    @XmlElement  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  

    @XmlAttribute  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  

    @Override  
    public String toString() {  
        return "Customer [id=" + id + ",name=" + name + ",age=" + age + "]";  
    }  
}  
第二步:编组(java对象转xml),提供多种编组目的地,以下只介绍两种,一种编组到文件,一种编组到控制台。
import java.io.File;  
import java.util.HashSet;  
import java.util.Set;  

import javax.xml.bind.JAXBContext;  
import javax.xml.bind.JAXBException;  
import javax.xml.bind.Marshaller;  

//Marshaller  
public class Object2XmlDemo {  
    public static void main(String[] args) {  

        Customer customer = new Customer();  
        customer.setId(100);  
        customer.setName("suo");  
        customer.setAge(29);  

        Book book = new Book();  
        book.setId("1");  
        book.setName("哈里波特");  
        book.setPrice(100);  

        Book book2 = new Book();  
        book2.setId("2");  
        book2.setName("苹果");  
        book2.setPrice(50);  

        Set<Book> bookSet = new HashSet<Book>();  
        bookSet.add(book);  
        bookSet.add(book2);  

        customer.setBook(bookSet);  

        try {  
            File file = new File("C:\\file1.xml");  
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
            // output pretty printed  
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
            jaxbMarshaller.marshal(customer, file);  
            jaxbMarshaller.marshal(customer, System.out);  
        } catch (JAXBException e) {  
            e.printStackTrace();  
        }  
    }  
} 

生成的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book id="100">
    <author>James</author>
    <calendar>2018-03-04T16:22:37.780+08:00</calendar>
    <price_1>23.45</price_1>
</book>

第三步:解组(xml转java对象),直接从文件解组xml成java对象。

import java.io.File;  
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.JAXBException;  
import javax.xml.bind.Unmarshaller;  

//Unmarshaller  
public class Xml2ObjectDemo {  
    public static void main(String[] args) {  
        try {  
            File file = new File("C:\\file.xml");  
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
            Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);  
            System.out.println(customer);  
        } catch (JAXBException e) {  
            e.printStackTrace();  
        }  
    }  
}  

注:以上代码在jdk1.6中运行正常,jdk1.5需要其它依赖包。

参考文档:

    http://blog.csdn.net/ruzhefeng/article/details/6560449  JAXB2简介及特性使用总结

    http://www.ibm.com/developerworks/cn/xml/x-pracdb1.html 实用数据绑定: 涉入现实世界

    http://www.ibm.com/developerworks/cn/xml/x-pracdb3.html 实用数据绑定: 深入考察 JAXB,第 2 部分



    http://jaxb.java.net/guide/

    http://jaxb.java.net/tutorial/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值