码迷,mamicode.com
首页 > 其他好文 > 详细

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

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

标签:style   blog   http   java   color   使用   io   2014   

 

处理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的转换

标签:style   blog   http   java   color   使用   io   2014   

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

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