jaxb常用的生成xml范例

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, true);
        
        //设置编码
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING,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>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值