JAXB--简单应用

一、简介

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模式。 


二、应用

java bean转换成xml

java对象:

package org.zj.user.java.javax.xml.bean;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.zj.user.util.base.BaseModel;

@XmlRootElement
public class Wife extends BaseModel {

    /**  */
    private static final long serialVersionUID = -8826556409418345873L;
    /**
     * Getter method for property <tt>uname</tt>.
     * 
     * @return property value of uname
     */
    @XmlElement
    public String getUname() {
        return uname;
    }

    /**
     * Setter method for property <tt>uname</tt>.
     * 
     * @param uname value to be assigned to property uname
     */
    public void setUname(String uname) {
        this.uname = uname;
    }

    /**
     * Getter method for property <tt>sex</tt>.
     * 
     * @return property value of sex
     */
    @XmlElement
    public String getSex() {
        return sex;
    }

    /**
     * Setter method for property <tt>sex</tt>.
     * 
     * @param sex value to be assigned to property sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * Getter method for property <tt>age</tt>.
     * 
     * @return property value of age
     */
    @XmlElement
    public int getAge() {
        return age;
    }

    /**
     * Setter method for property <tt>age</tt>.
     * 
     * @param age value to be assigned to property age
     */
    public void setAge(int age) {
        this.age = age;
    }

    private String uname;
    private String sex;
    private int    age;
}

package org.zj.user.java.javax.xml.bean;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.zj.user.util.base.BaseModel;

@XmlRootElement
public class Husband extends BaseModel {

    /**  */
    private static final long serialVersionUID = 1982111522513879915L;

    /**
     * Getter method for property <tt>username</tt>.
     * 
     * @return property value of username
     */
    @XmlElement
    public String getUsername() {
        return username;
    }

    /**
     * Setter method for property <tt>username</tt>.
     * 
     * @param username value to be assigned to property username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Getter method for property <tt>sex</tt>.
     * 
     * @return property value of sex
     */
    @XmlElement
    public String getSex() {
        return sex;
    }

    /**
     * Setter method for property <tt>sex</tt>.
     * 
     * @param sex value to be assigned to property sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * Getter method for property <tt>age</tt>.
     * 
     * @return property value of age
     */
    @XmlElement
    public int getAge() {
        return age;
    }

    /**
     * Setter method for property <tt>age</tt>.
     * 
     * @param age value to be assigned to property age
     */
    public void setAge(int age) {
        this.age = age;
    }

    private String username;
    private String sex;
    private int    age;
    private Wife   wife;

    /**
     * Getter method for property <tt>wife</tt>.
     * 
     * @return property value of wife
     */
    @XmlElement
    public Wife getWife() {
        return wife;
    }

    /**
     * Setter method for property <tt>wife</tt>.
     * 
     * @param wife value to be assigned to property wife
     */
    public void setWife(Wife wife) {
        this.wife = wife;
    }
}


测试类:

 @Test
    public void test_bean_to_xml() {

        Wife wife = new Wife();
        wife.setAge(20);
        wife.setSex("女");
        wife.setUname("marry");
        Husband p = new Husband();
        p.setAge(12);
        p.setSex("男");
        p.setUsername("张三");
        p.setWife(wife);

        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(Husband.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            // output pretty printed  
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(p, System.out);//转换成xml
        } catch (JAXBException e) {
            logger.error("xxxxx", e);
        }
    }
结果:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<husband>
    <age>12</age>
    <sex>男</sex>
    <username>张三</username>
    <wife>
        <age>20</age>
        <sex>女</sex>
        <uname>marry</uname>
    </wife>
</husband>



xml转成java对象

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<husband>
    <age>12</age>
    <sex>男</sex>
    <username>张三</username>
    <wife>
        <age>20</age>
        <sex>Ů</sex>
        <uname>marry</uname>
    </wife>
</husband>

测试方法

 

 @Test
    public void test_xml_to_bean() {

        File file = new File("E:\\husband.xml");
        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(Husband.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Husband husband = (Husband) jaxbUnmarshaller.unmarshal(file);
            logger.info("xxxx{}", husband);
        } catch (JAXBException e) {
            logger.error("", e);
        }
    }


测试结果:

2015-09-08 15:02:37,847 [main] [@  ] INFO  org.zj.user.java.javax.xml.XmlBeanTest [XmlBeanTest.java:53-test_xml_to_bean] - xxxxorg.zj.user.java.javax.xml.bean.Husband@5b266925[
  username=张三
  sex=男
  age=12
  wife=org.zj.user.java.javax.xml.bean.Wife@60869739[
  uname=marry
  sex=Ů
  age=20
]
]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值