package org.itat.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;
public class TestJaxb {
//编排
public static void test(String[] args) throws Exception {
// TODO Auto-generated method stub
JAXBContext ctx=JAXBContext.newInstance(Student.class);
Marshaller marshaller=ctx.createMarshaller();
Student stu=new Student();
stu.setAge(11);
stu.setId(1);
stu.setName("Aro");
Classroom classrooml=new Classroom();
classrooml.setGrade(11);
classrooml.setId(1);
classrooml.setName("计算机科学与技术");
stu.setClassrooml(classrooml);
marshaller.marshal(stu, System.out);
}
//反编排
public static void main(String[] args) throws Exception {
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>11</age><classrooml><grade>11</grade><id>1</id><name>计算机科学与技术</name></classrooml><id>1</id><name>Aro</name></student>";
JAXBContext ctx=JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller=ctx.createUnmarshaller();
Student student=(Student) unmarshaller.unmarshal(new StringReader(xml));
System.out.println(student.getClassrooml().getName());
}
}
xml-xml与java对象之间的转换(jaxb)
最新推荐文章于 2024-09-04 11:02:51 发布