java和XML转换

转载 2018年04月16日 17:33:27

用的是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

xml与Java对象的转换

1.xstream解析报文 XStreamComponent x = XStreamComponent.newInstance();  x.processAnnotations(new Class...
  • u014401141
  • u014401141
  • 2016-12-30 09:17:41
  • 448

Xml转换为java List

package com.netstar8.dao.test;   import java.io.File; import java.io.IOException; import java.ut...
  • familyGo
  • familyGo
  • 2011-08-15 00:22:20
  • 1135

java中使用xslt转换xml成为另一个xml

  • 2009年07月06日 10:04
  • 2KB
  • 下载

xStream.jar 资源包

  • 2010年08月05日 09:38
  • 418KB
  • 下载

XStream: Java对象和XML之间相互转换的利器

XStream是个好东西,可以方便在Java对象和XML之间相互转换,效率和可读性都非常高,下面是我写的一个测试的例子,放出来一块看看。   需要的jar包如下: xpp3_min-...
  • bigtree_3721
  • bigtree_3721
  • 2016-09-17 18:16:18
  • 550

xml、xsd、javabean的互相转换

  • 2014年07月29日 17:30
  • 908KB
  • 下载

Java中XSLT转换的简单实例

package com.huey.dream.util; import java.io.FileNotFoundException; import java.io.FileOutputStream;...
  • huey2672
  • huey2672
  • 2014-08-22 23:31:39
  • 1521

java关于对象(javabean)与xml之间的相互转换

今天我们要探讨的一个问题是,如何将xml格式的字符串转换成对象,以及对象转换成xml字符串。 简介 现如今,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行...
  • liboyang71
  • liboyang71
  • 2017-06-14 17:21:01
  • 7561

Java与XML(四)xml的转换之html

Xml转换是个必要而且常见的工作。这里将给出xml转换成html的例子:myxml.xml:   a  aa    a1  aa1 myxsl.xsl:xmlns:xsl="http://www....
  • delva
  • delva
  • 2005-01-17 17:23:00
  • 2688

java 中xml转换为json对象

欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和...
  • u012149894
  • u012149894
  • 2015-07-02 10:52:40
  • 1108
收藏助手
不良信息举报
您举报文章:java和XML转换
举报原因:
原因补充:

(最多只允许输入30个字)