JAXB(xml和对象的转换)

时间:2014-01-24 19:43:01   收藏:0   阅读:373

1、编排(将java转换成xml)

2、反编排(将xml转换成java)

首先先编排(要在编排的类上面用一个注解)

            @XmlRootElement
bubuko.com,布布扣
            JAXBContext ctx=JAXBContext.newInstance(Student.class);//jdk1.6自带
            Student stu=new Student(1,"李阳","24",new ClassRoom(10,"13信息与计算科学",2013));
            Marshaller marshaller=ctx.createMarshaller();//编排
            marshaller.marshal(stu, System.out);
bubuko.com,布布扣

反编排

bubuko.com,布布扣
               String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>24</age><classroom><gra                       de>2013</grade><id>10</id><name>13信息与计算科学</name></classroom><id>1</id><name>李阳</name></student>";
               JAXBContext ctx=JAXBContext.newInstance(Student.class);//jdk1.6自带
               Unmarshaller unmarshaller=ctx.createUnmarshaller();//反编排
               Student stu=(Student) unmarshaller.unmarshal(new StringReader(xml));
               System.out.println(stu.getName());
bubuko.com,布布扣

原文:http://www.cnblogs.com/liyang31tg/p/3532151.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!