仪一

尽人事以格天心而

java和XML转换(一)

用的是jdk自带的javax.xml.bind.JAXBContext将对象和xml字符串进行相互转换。

比较常用的几个:

  @XmlRootElement:根节点
  @XmlAttribute:该属性作为xml的attribute
  @XmlElement:该属性作为xml的element,且可以增加属性(name=”NewElementName”),那么生成的xml串的elment的标签是NewElementName

首先,java注解读取XML。
一、新建school.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<SCHOOL>
    <name>延边大学</name>
    <address>吉林延吉市</address>
    <student grade="1">
        <num>001</num>
        <name>小杜</name>
        <age>23</age>
    </student>
    <student grade="2">
        <num>002</num>
        <name>小张</name>
        <age>21</age>
    </student>
</SCHOOL>

二、根据配置文件书写对应的注解类。

  编写Student基类,内容如下:
  

package com.cvicse.ump.xml.school;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

public class Student {


    private String studentNum;
    private String studentName;
    private String studentGrade;
    private int age;

    @XmlElement(name="num")
    public String getStudentNum() {
        return studentNum;
    }
    public void setStudentNum(String studentNum) {
        this.studentNum = studentNum;
    }
    @XmlElement(name="name")
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    @XmlAttribute(name="grade")
    public String getStudentGrade() {
        return studentGrade;
    }
    public void setStudentGrade(String studentGrade) {
        this.studentGrade = studentGrade;
    }
    @XmlElement
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [studentNum=" + studentNum + ", studentName="
                + studentName + ", studentGrade=" + studentGrade + ", age="
                + age + "]";
    }



}

编写基类school,内容如下:

package com.cvicse.ump.xml.school;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="SCHOOL")
public class School {

    private String name;
    private String address;
    private List<Student>students;

    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @XmlElement(name="student")
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    @Override
    public String toString() {
        return "School [name=" + name + ", address=" + address + "]";
    }

}

编写XML读取程序:

package com.cvicse.ump.xml.school;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class SchoolRead {

    public static void main(String[] args) {

        File file = new File("src/school.xml");
        try {
            JAXBContext context = JAXBContext.newInstance(School.class);
            Unmarshaller unMar = context.createUnmarshaller();
            School school = (School) unMar.unmarshal(file);
            System.out.println(school);
            List<Student>students = school.getStudents();
            for(Student student:students){
                System.out.println(student);
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

编写写入文件程,内容如下

package com.cvicse.ump.xml.school;

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 SchoolWrite {

    public static void main(String[] args) {

        School school = new School();
        school.setName("实验小学");
        school.setAddress("山东济南市");

        List<Student> students = new ArrayList<Student>();
        Student student1 = new Student();
        student1.setStudentNum("001");
        student1.setStudentName("xiaoDy");
        student1.setAge(25);
        student1.setStudentGrade("1");

        Student student2 = new Student();
        student2.setStudentNum("002");
        student2.setStudentName("xiaoWf");
        student2.setAge(22);
        student2.setStudentGrade("2");

        students.add(student1);
        students.add(student2);
        school.setStudents(students);
        try {
            JAXBContext context = JAXBContext.newInstance(School.class);
            Marshaller mar = context.createMarshaller();
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.marshal(school, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }


    }

}

转载地址 https://www.cnblogs.com/dyh004/p/5220116.html

阅读更多
个人分类: java
上一篇java 北京时间和世界时的转换
下一篇java和XML转换(二)
想对作者说点什么? 我来说一句

xStream.jar 资源包

2010年08月05日 418KB 下载

XMLJava基础

2011年12月19日 693KB 下载

xmljava对象互相转换的两种方法

2014年05月01日 3.83MB 下载

没有更多推荐了,返回首页

关闭
关闭