码迷,mamicode.com
首页 > 编程语言 > 详细

使用 JDK XML 和 java对象相互转换

时间:2017-10-18 00:14:41      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:对象   cep   win   instance   使用   div   mode   for   return   

Unmarshaller 类能将 XML 数据转换为 Java 内容对象。

Marshaller 类能够将 Java 对象转换回 XML 数据。

package jaxb;

/**
 * Created by sheting on 10/17/2017
 */
public class Classroom {
    private int id;
    private String name;
    private int grade;

    public Classroom() {
    }

    public Classroom(int id, String name, int grade) {
        super();
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }
}
package jaxb;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * Created by sheting on 10/17/2017
 */
@XmlRootElement
public class Student {
    private int id;
    private String name;
    private int age;
    private Classroom classroom;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Classroom getClassroom() {
        return classroom;
    }

    public void setClassroom(Classroom classroom) {
        this.classroom = classroom;
    }

    public Student(int id, String name, int age, Classroom classroom) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.classroom = classroom;
    }

    //无参够着函数一定需要,否则JXBContext无法正常解析。
    public Student() {
        super();
    }
}
package jaxb;

import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * Created by sheting on 10/17/2017
 */
public class TestJaxb {

    @Test
    public void beanToXML() {
        Classroom classroom = new Classroom(1, "软件工程", 4);
        Student student = new Student(101, "张三", 22, classroom);

        try {
            JAXBContext context = JAXBContext.newInstance(Student.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.marshal(student, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void XMLStringToBean(){
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>22</age><classroom><grade>4</grade><id>1</id><name>软件工程</name></classroom><id>101</id><name>张三</name></student>";
        try {
            JAXBContext context = JAXBContext.newInstance(Student.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr));
            System.out.println(student.getAge());
            System.out.println(student.getClassroom().getName());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

注意:

1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象则不需要

2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。

 

        JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。

    JAXBContext 类提供到 JAXB API 的客户端入口点。它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。

原文:http://hbiao68.iteye.com/blog/1958413

 

使用 JDK XML 和 java对象相互转换

标签:对象   cep   win   instance   使用   div   mode   for   return   

原文地址:http://www.cnblogs.com/zheting/p/7684401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!