JDK6的新特性之二: JAXB2

  使用JAXB2来实现对象与XML之间的映射 JAXBJava Architecture for XML Binding的缩写,先来说一说这个是用来做什么的吧。这个工具就像我们生活中神奇的温度。它可以把水把变成冰,也可以把冰变成水。而这个工具就像温度一样的神奇可以把java的对象和xml文件进行互转。你很聪明,也就是说可把xml文件转为java对象,也可以把java对象转为xml文件。一种对象与关系数据库之间的映射称为ORM,聪明的你就会想到可以把对象和xml之间的映射称为OXM(Object XML Mapping). Xml文件在j2ee开发中使用得就每天要吃那样的频繁。Sun还老套把这个工具放到了j2se中去了。这里说到的是2.0那你一定会也想到有一个1.0吧。那就说一说2.01.0的区别吧。很对不仅有一字之差12之差。还有2.0加入了一些功能。JAXB2(JSR 222)JDK5的新特性Annotation来标识要作绑定的类和属性等,这就极大简化了开发的工作量。闲话不多说太多了,说多了你就会觉得没完没了 

好开始看一看下面这个实例吧。

package jaxb2;

 

import java.io.FileReader;

import java.io.FileWriter;

import java.util.Calendar;

 

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

import javax.xml.bind.annotation.XmlRootElement;

public class JAXB2Test {

 

       /**

        * @param args

        * @throws Exception

        */

       public static void main(String[] args) throws Exception {

              // TODO Auto-generated method stub

              JAXBContext context = JAXBContext.newInstance(Student.class);

              //下面代码演示将对象转变为xml

              Marshaller m = context.createMarshaller();

              Address address = new Address("China","Beijing","Beijing","HuiLongGuan West","20090806");

              Student p = new Student(200800,"leiwuluan",address,95.5,1);

              FileWriter fw = new FileWriter("Student.xml");

              m.marshal(p,fw);

 

              //下面代码演示将上面生成的xml转换为对象

              FileReader fr = new FileReader("person.xml");

              Unmarshaller um = context.createUnmarshaller();

              Student p2 = (Student)um.unmarshal(fr);

              System.out.println("Country:"+p2.getAddress().getCountry());

       }

}

 

 

@XmlRootElement//表示person是一个根元素

class Student {   

    @javax.xml.bind.annotation.XmlElement

    Integer studentId; //studentId将作为Student的子元素

 

    @javax.xml.bind.annotation.XmlAttribute

    String studentName; //studentName将作为Student的的一个属性

 

    @javax.xml.bind.annotation.XmlElement

    Address address; //address将作为Student的子元素

 

    @javax.xml.bind.annotation.XmlElement

    Double score; //gender将作为Student的子元素

 

    @javax.xml.bind.annotation.XmlElement

    Integer grade; //job将作为Student的子元素

 

    public Student(){

    }

  

       public Student(Integer studentId, String studentName, Address address,

                     Double score, Integer grade) {

              super();

              this.studentId = studentId;

              this.studentName = studentName;

              this.address = address;

              this.score = score;

              this.grade = grade;

       }

 

       public Address getAddress() {

        return address;

    }

}

 

class Address {

    @javax.xml.bind.annotation.XmlAttribute

    String country;

    @javax.xml.bind.annotation.XmlElement

    String state;

    @javax.xml.bind.annotation.XmlElement

    String city;

    @javax.xml.bind.annotation.XmlElement

    String street;

    String zipcode; //由于没有添加@XmlElement,所以该元素不会出现在输出的xml

 

    public Address() {

    }

 

    public Address(String country, String state, String city, String street, String zipcode) {

        this.country = country;

        this.state = state;

        this.city = city;

        this.street = street;

        this.zipcode = zipcode;

    }

 

 

    public String getCountry() {

        return country;

    }

}

 

都说将对象转为xml文件了。所以你就可以得到一个名为Student.xml文件了。里面的内容不用猜的跟平常文件一样的。就像下面一模一样的。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<student studentName="leiwuluan">

    <studentId>200800</studentId>

    <address country="China">

       <state>Beijing</state>

       <city>Beijing</city>

       <street>HuiLongGuan West</street>

    </address>

    <score>95.5</score>

    <grade>1</grade>

</student>

 

做到这里聪明的你也会了吧。你一定想问还有没有别的东西也可以实现这个呢。

你不问我也会对你说还有,真的有。这只是其中的一种而以。

XMLBeansCastor

这两个也可以。

手累了不多写了。认真看一遍就会了。不要跳页去看,如果这样你永远也不会。

看书时,你说你看不懂。其实你不是看不懂,而是你没有用心看。而是上页没有看就看下一而,记住看书不是在跳台阶,可以隔着跳。。。不说多了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值