package com.br.collectionai.web.controller.baiying;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XmlToBean {
/**
* xml文件配置转换为对象
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return java对象
* @throws JAXBException
* @throws IOException
*/
public static Object xmlToBean(String xmlPath,Class<?> load) throws JAXBException, IOException{
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlPath);
Object xmlObject = unmarshaller.unmarshal(sr);
return xmlObject;
}
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
public static void main(String[] args) throws IOException, JAXBException {
// String xmlPath = "D:/testConfig.xml";
String xmlPath = "<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
" +
"<list>
" +
" <student name="张三" number="10001" sex="男">
" +
" <className>尖子班</className>
" +
" <hobbys>
" +
" <hobby>篮球</hobby>
" +
" <hobby>音乐</hobby>
" +
" <hobby>乒乓球</hobby>
" +
" </hobbys>
" +
" </student>
" +
" <student name="李四" number="10002" sex="男">
" +
" <className>普通班</className>
" +
" <hobbys>
" +
" <hobby>篮球</hobby>
" +
" <hobby>音乐</hobby>
" +
" <hobby>乒乓球</hobby>
" +
" </hobbys>
" +
" </student>
" +
" <student name="莉莉" number="10003" sex="女">
" +
" <className>普通班</className>
" +
" <hobbys>
" +
" <hobby>篮球</hobby>
" +
" <hobby>音乐</hobby>
" +
" <hobby>乒乓球</hobby>
" +
" </hobbys>
" +
" </student>
" +
"</list>";
Object object = XmlToBean.xmlToBean(xmlPath,StudentList.class);
StudentList students = (StudentList)object;
List<Student> studentList = students.getStudents();
for(int i=0;i<studentList.size();i++){
System.out.println(studentList.get(i).name);
System.out.println(studentList.get(i).sex);
System.out.println(studentList.get(i).number);
System.out.println(studentList.get(i).className);
for(String str :studentList.get(i).hobby){
System.out.print(str+" ");
}
System.out.println("-------------");
}
}
}
package com.br.collectionai.web.controller.baiying;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
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 BeanToXml {
/**
* java对象转换为xml文件
* @param load java对象.Class
* @return xml文件的String
* @throws JAXBException
*/
public static String beanToXml(Object obj,Class<?> load) throws JAXBException{
JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter writer = new StringWriter();
marshaller.marshal(obj,writer);
return writer.toString();
}
public static void main(String[] args) throws JAXBException, IOException {
List<String> hobby = new ArrayList<>();
hobby.add("篮球");
hobby.add("音乐");
hobby.add("乒乓球");
List<Student> studentList = new ArrayList<>();
Student st = new Student("张三","男",10001,"尖子班",hobby);
studentList.add(st);
Student st1 = new Student("李四","男",10002,"普通班",hobby);
studentList.add(st1);
Student st2 = new Student("莉莉","女",10003,"普通班",hobby);
studentList.add(st2);
StudentList students = new StudentList();
students.setStudents(studentList);
String str = BeanToXml.beanToXml(students, StudentList.class);
System.out.println(str);
//写入到xml文件中
String xmlPath = "D:/testConfig.xml";
BufferedWriter bfw = new BufferedWriter(new FileWriter(new File(xmlPath)));
bfw.write(str);
bfw.close();
}
}
package com.br.collectionai.web.controller.baiying;
/**
* @ClassName Student
* @Description
* @Author jiajun.cao
* @Date 2020-11-27 14:45
**/
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElementWrapper;
public class Student {
String name; //姓名
String sex; //性别
int number; //学号
String className; //班级
List<String> hobby; //爱好
public Student() {
}
public Student(String name, String sex, int number,
String className, List<String> hobby) {
this.name = name;
this.sex = sex;
this.number = number;
this.className = className;
this.hobby = hobby;
}
@XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute(name = "sex")
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@XmlAttribute(name = "number")
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@XmlElement(name = "className")
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@XmlElementWrapper(name = "hobbys")
@XmlElement(name = "hobby")
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
}
package com.br.collectionai.web.controller.baiying;
/**
* @ClassName StudentList
* @Description
* @Author jiajun.cao
* @Date 2020-11-27 14:48
**/
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "list")
public class StudentList {
List<Student> students; //所有学生信息的集合
@XmlElement(name = "student")
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}