首页 > 其他 > 详细

09_使用jaxb完成对象和xml的转换

时间:2014-07-29 11:42:36      阅读:299      评论:0      收藏:0      [点我收藏+]

 

处理XML的方式:

  SAX

  dom4j

  Xstream-->stax

  XStream,Jackson

  JAXB:

JAXB实例:

package com.bling.xml;

import java.io.StringReader;

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

import org.junit.Test;

public class TestJaxb {
    @Test
    public void test01(){
        try {
            JAXBContext ctx = JAXBContext.newInstance(Student.class);
            Marshaller marshaller = ctx.createMarshaller();
            Student stu = new Student(1,"杨明亮",21,new Classroom(1,"计算机",2014));
            marshaller.marshal(stu, System.out);
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Test
    public void test02(){
        String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>21</age><classroom><grade>2014</grade><id>1</id><name>计算机</name></classroom><id>1</id><name>杨明亮</name></student>";
        JAXBContext ctx;
        try {
            ctx = JAXBContext.newInstance(Student.class);
            Unmarshaller um = ctx.createUnmarshaller();
            Student stu = (Student)um.unmarshal(new StringReader(xml));
            System.out.println("student name:"+stu.getName());
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

bubuko.com,布布扣

GitHub源码地址:https://github.com/WebServcie/04_java_xml

 

09_使用jaxb完成对象和xml的转换,布布扣,bubuko.com

09_使用jaxb完成对象和xml的转换

原文:http://www.cnblogs.com/yangml/p/3866716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!