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,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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


xml怎么加入图片路径
rss源错误怎么解决
文件后缀xml是什么意思
xml格式电子发票怎么获取
xml格式是什么意思
rss是什么意思啊
xml格式电子发票怎么打开
rss订阅源是什么意思
rss源是什么
xml注释怎么写
php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类