Student.java
package com.jaxb; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; @XmlAccessorType(XmlAccessType.FIELD) public class Student { //作为节点的一个属性<student id="1"> @XmlAttribute private int id; @XmlElement(name="stAge") private int studentAge; @XmlElement(name="stName") private String studentName; public Student(){ } /** * 创建一个新的实例Student. * @param id * @param studentAge * @param studentName */ public Student(int id,int studentAge,String studentName) { super(); this.id = id; this.studentAge = studentAge; this.studentName = studentName; } /** * 获取studentAge * @return studentAge studentAge */ public int getStudentAge() { return studentAge; } /** * 设置studentAge * @param studentAge studentAge */ public void setStudentAge(int studentAge) { this.studentAge = studentAge; } /** * 获取studentName * @return studentName studentName */ public String getStudentName() { return studentName; } /** * 设置studentName * @param studentName studentName */ public void setStudentName(String studentName) { this.studentName = studentName; } /** * 获取id * @return id id */ public int getId() { return id; } /** * 设置id * @param id id */ public void setId(int id) { this.id = id; } }
Teacher.java
package com.jaxb; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class Teacher{ @XmlElement(name="teAge") private int teacherAge; @XmlElement(name="teName") private String teacherName; //XmlElementWrapper这个会给xml包装一层 //<students> // <student> // ...... // </student> // ...... //</students> @XmlElementWrapper(name="students") @XmlElement(name="student") private List<Student> students; public Teacher(){} /** * 创建一个新的实例Teacher. * @param teacherAge * @param teacherName */ public Teacher(int teacherAge,String teacherName) { super(); this.teacherAge = teacherAge; this.teacherName = teacherName; } /** * 获取teacherAge * @return teacherAge teacherAge */ public int getTeacherAge() { return teacherAge; } /** * 设置teacherAge * @param teacherAge teacherAge */ public void setTeacherAge(int teacherAge) { this.teacherAge = teacherAge; } /** * 获取teacherName * @return teacherName teacherName */ public String getTeacherName() { return teacherName; } /** * 设置teacherName * @param teacherName teacherName */ public void setTeacherName(String teacherName) { this.teacherName = teacherName; } /** * 获取students * @return students students */ public List<Student> getStudents() { return students; } /** * 设置students * @param students students */ public void setStudents(List<Student> students) { this.students = students; } }
JaxbTest.java
package com.jaxb; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class JaxbTest { private final static String XML_CODE = "UTF-8"; public static void main(String[] args) throws JAXBException { Teacher teacher = new Teacher(33,"teacherA"); List<Student> students = new ArrayList<Student>(); for (int i = 1; i < 3; i++) { Student student = new Student(i,i,"sName_"+i); students.add(student); } teacher.setStudents(students); //存到字符串中并打印出来 System.out.println(objectToXmlStr(teacher)); //写入到文件中 objectToXmlToFile(teacher,"d:\\teacher.xml"); //打印到控制台 objectToXmlToConsle(teacher); } public static void objectToXmlToConsle(Object object) throws JAXBException { if(object == null) return; System.out.println("打印xml到控制台"); JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = context.createMarshaller(); //是否格式化输出xml文件 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); //设置编码 jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING,XML_CODE); //控制台打印输出 jaxbMarshaller.marshal(object,System.out); } public static void objectToXmlToFile(Object object,String filePath) throws JAXBException { if(object == null || filePath == null || "".equals(filePath)) return; File file = new File(filePath); JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = context.createMarshaller(); //是否格式化输出xml文件 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); //设置编码 jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING,XML_CODE); //写入到文件中 jaxbMarshaller.marshal(object,file); System.out.println("xml已写入到文件"+filePath); } public static String objectToXmlStr(Object object) throws JAXBException { if (object == null) return ""; System.out.println("把对象转换为xml字符串"); OutputStream out = new ByteArrayOutputStream(); JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = context.createMarshaller(); //是否格式化输出xml文件 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,XML_CODE); //打印到输出流中 jaxbMarshaller.marshal(object,out); return out.toString(); } }
打印到控制台的xml内容为
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <teacher> <teAge>33</teAge> <teName>teacherA</teName> <students> <student id="1"> <stAge>1</stAge> <stName>sName_1</stName> </student> <student id="2"> <stAge>2</stAge> <stName>sName_2</stName> </student> </students> </teacher>原文链接:https://www.f2er.com/xml/297964.html