JDK6 的新特性之二:使用JAXB2来实现对象与XML之间的映射

JAXB是Java Architecture for XML Binding 的缩写,可以将一个Java对象转变成XML格式,反之亦然。

 

package com.java6.test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JAXB2Tester
{
    public static void main(String[] args) throws JAXBException, IOException
    {

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

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

        Marshaller m = context.createMarshaller();

        Address address = new Address("China", "Beijing", "Beijing", "ShangDi West", "100080");

        Person p = new Person(Calendar.getInstance(), "JAXB2", address, Gender.MALE, "SW");

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

        m.marshal(p, fw);

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

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

        Unmarshaller um = context.createUnmarshaller();

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

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

}

@XmlRootElement
// 表示person 是一个根元素
class Person
{
    @XmlElement
    Calendar birthDay; // birthday 将作为person 的子元素

    @XmlAttribute
    String name; // name 将作为person 的的一个属性

    public Address getAddress()
    {
        return address;
    }

    @XmlElement
    Address address; // address 将作为person 的子元素

    @XmlElement
    Gender gender; // gender 将作为person 的子元素

    @XmlElement
    String job; // job 将作为person 的子元素

    public Person()
    {

    }

    public Person(Calendar birthDay, String name, Address address, Gender gender, String job)
    {
        this.birthDay = birthDay;
        this.name = name;
        this.address = address;
        this.gender = gender;
        this.job = job;
    }
}

enum Gender
{

    MALE(true),

    FEMALE(false);

    private boolean value;

    Gender(boolean _value)
    {

        value = _value;

    }

}

class Address
{

    @XmlAttribute
    String country;

    @XmlElement
    String state;

    @XmlElement
    String city;

    @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;
    }
}

 

person.xml

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person name="JAXB2">
    <birthDay>2010-04-12T22:40:24.625+08:00</birthDay>
    <address country="China">
        <state>Beijing</state>
        <city>Beijing</city>
        <street>ShangDi West</street>
    </address>
    <gender>MALE</gender>
    <job>SW</job>
</person>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值