首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

运用JAXB完成XML与java对象的转换

2013-08-04 
使用JAXB完成XML与java对象的转换SAX处理超大xml时使用 dom4j通过xpath来解析非常方便,适合处理较小的xmlX

使用JAXB完成XML与java对象的转换

SAX             处理超大xml时使用
dom4j           通过xpath来解析非常方便,适合处理较小的xml
Xstream--->Stax 基于流的方式处理
Xstream,Jackson 可以将json与java进行转换
JAXB java表中API,专门负责java与xml的互转
java--->xml:marshaller
xml--->java:unmarshaller


JAVB完成对象与XML的互转

package com.hqh.jaxb;public class Classroom {private int id;private String name;private int grad;public Classroom() {super();}public Classroom(int id, String name, int grad) {super();this.id = id;this.name = name;this.grad = grad;}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 getGrad() {return grad;}public void setGrad(int grad) {this.grad = grad;}}


package com.hqh.jaxb;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Student {private int id;private String name;private int age;private String sex;private Classroom classroom;public Student() {super();}public Student(int id, String name, int age, String sex, Classroom classroom) {super();this.id = id;this.name = name;this.age = age;this.sex = sex;this.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 String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Classroom getClassroom() {return classroom;}public void setClassroom(Classroom classroom) {this.classroom = classroom;}}


package com.hqh.jaxb;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import org.junit.Test;public class JAXBTest {/**Object--->XML*/@Testpublic void testMarshaller() throws Exception {//对象//要使用JAXB转换对象为xml,需要在对应的类上使用@XmlRootElement进行标注Classroom classroom = new Classroom(1, "一班", 2013);Student jaxbElement = new Student(1, "张三", 22, "男", classroom);//将student对象转为xmlJAXBContext ctx = JAXBContext.newInstance(Student.class);Marshaller marshaller = ctx.createMarshaller();//将转换后的xml作为标准打印流输出marshaller.marshal(jaxbElement, System.out);//将转换后的xml以文件形式输出File file = new File("src/student.xml");if(!file.exists()) {file.createNewFile();}marshaller.marshal(jaxbElement, new FileOutputStream(file, false));}/**XML--->Object*/@Testpublic void testUnMarshaller() throws Exception {//XMLInputStream is = this.getClass().getClassLoader().getResourceAsStream("student.xml");//将student对象转为xml转为student对象JAXBContext ctx = JAXBContext.newInstance(Student.class);Unmarshaller unmarshaller = ctx.createUnmarshaller();Object obj = unmarshaller.unmarshal(is);Student stu = (Student)obj;System.out.println(stu.getName()+","+stu.getClassroom().getName());}}


将Student对象转换为xml的结果,很方便
如果存在中文转换,将项目的编码设置为UTF-8,不然中文为乱码
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><student><age>22</age><classroom><grad>2013</grad><id>1</id><name>一班</name></classroom><id>1</id><name>张三</name><sex>男</sex></student>

热点排行