写点什么

JavaBean 与 xml 互转的方法详解

  • 2021 年 11 月 11 日
  • 本文字数:3239 字

    阅读完需:约 11 分钟

import javax.xml.bind.JAXBContext;


import javax.xml.bind.Marshaller;


import javax.xml.bind.Unmarshaller;


public class JaxbUtil {


/**


  • JavaBean 装换成 xml 默认编码 UTF-8

  • @param obj

  • @return


*/


public static String converTomXml(Object obj) {


return converToXml(obj, "UTF-8");


}


/**


  • JavaBean 装换成 xml

  • @param obj

  • @param encoding

  • @return


*/


private static String converToXml(Object obj, String encoding) {


String result = null;


try {


JAXBContext context = JAXBContext.newInstance(obj.getClass());


Marshaller marshaller = context.createMarshaller();


marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);


StringWriter writer = new StringWriter();


marshaller.marshal(obj, writer);


result = writer.toString();


} catch (Exception e) {


e.printStackTrace();


}


return result;


}


/**


  • xml 装换成 JavaBean

  • @param xml

  • @param c

  • @return


*/


@SuppressWarnings("unchecked")


public static <T> T converyToJavaBean(String xml, Class<T> c) {


T t = null;


try {


JAXBContext context = JAXBContext.newInstance(c);


Unmarshaller unmarshaller = context.createUnmarshaller();


t = (T) unmarshaller.unmarshal(new StringReader(xml));


} catch (Exception e) {


e.printStackTrace();


}


return t;


}


}

2、简单类型?

(1)单实体类转换


package com.guor.demo.beanToXml;


import java.util.Date;


import javax.xml.bind.annotation.XmlAccessType;


import javax.xml.bind.annotation.XmlAccessorType;


import javax.xml.bind.annotation.XmlAttribute;


import javax.xml.bind.annotation.XmlElement;


import javax.xml.bind.annotation.XmlRootElement;


import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)


@XmlRootElement


@XmlType(name = "book", propOrder = { "author", "calendar", "price", "id" })


public class Book {


@XmlElement(required = true)


private String author;


@XmlElement(name = "price_1", required = true)


private float price;


@XmlElement


private Date calenda


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


r;


@XmlAttribute


private Integer id;


/**


  • @return


*/


public String getAuthor() {


return author;


}


public void setAuthor(String author) {


this.author = author;


}


public float getPrice() {


return price;


}


public void setPrice(float price) {


this.price = price;


}


public Date getCalendar() {


return calendar;


}


public void setCalendar(Date calendar) {


this.calendar = calendar;


}


public Integer getId() {


return id;


}


public void setId(Integer id) {


this.id = id;


}


@Override


public String toString() {


return "Book [author=" + author + ", price=" + price + ", calendar=" + calendar + ", id=" + id + "]";


}


}


(2)test?


package com.guor.demo.beanToXml;


import java.util.Date;


import org.junit.Test;


import javax.xml.bind.JAXBException;


@SuppressWarnings("unused")


public class JaxbTest1 {


/**


  • @throws JAXBException


*/


@Test


public void showMarshaller() {


Book book = new Book();


book.setId(100);


book.setAuthor("lin");


book.setCalendar(new Date());


book.setPrice(23.56f);


String str = JaxbUtil.converTomXml(book);


System.out.println(str);


}


/**


  • @throws JAXBException


*/


@Test


public void showUnMarshaller() {


String str = "<?xml version ="1.0" encoding="UTF-8" standalone="yes"?>" + "<book id ="100">"


  • "<author>lin</author>" + "<calendar>2018-9-3T11:58.006</calendar>" + "<price_1>23.56</price_1>"

  • "</book>";


Book book = JaxbUtil.converyToJavaBean(str, Book.class);


System.out.println(book);


}


}


(3)控制台输出


3、类中包含复杂对象的转换

(1)实体类 1


package com.guor.demo.beanToXml;


import javax.xml.bind.annotation.XmlAccessType;


import javax.xml.bind.annotation.XmlAccessorType;


import javax.xml.bind.annotation.XmlAttribute;


import javax.xml.bind.annotation.XmlElement;


import javax.xml.bind.annotation.XmlRootElement;


import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)


@XmlRootElement(name = "student")


@XmlType(propOrder = {})


public class Student {


@XmlAttribute


private Integer id;


@XmlElement


private String name;


@XmlElement(name = "role")


private Role role;


public Integer getId() {


return id;


}


public void setId(Integer id) {


this.id = id;


}


public String getName() {


return name;


}


public void setName(String name) {


this.name = name;


}


public Role getRole() {


return role;


}


public void setRole(Role role) {


this.role = role;


}


@Override


public String toString() {


return "Student [id=" + id + ", name=" + name + ", role=" + role + "]";


}


}


(2)实体类 2?


package com.guor.demo.beanToXml;


import javax.xml.bind.annotation.XmlAccessType;


import javax.xml.bind.annotation.XmlAccessorType;


import javax.xml.bind.annotation.XmlElement;


import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)


@XmlType(propOrder = { "name", "desc" })


public class Role {


@XmlElement


private String name;


@XmlElement


private String desc;


public String getName() {


return name;


}


public void setName(String name) {


this.name = name;


}


public String getDesc() {


return desc;


}


public void setDesc(String desc) {


this.desc = desc;


}


@Override


public String toString() {


return "Role [name=" + name + ",desc=" + desc + "]";


}


}


(3)test?


package com.guor.demo.beanToXml;


import org.junit.Test;


public class JaxbTest2 {


@Test


public void showMarshaller() {


Student student = new Student();


student.setId(12);


student.setName("nihao");


Role role = new Role();


role.setDesc("管理");


role.setName("班长");


student.setRole(role);


String str = JaxbUtil.converTomXml(student);


System.out.println(str);


}


@Test


public void showUnMarshaller() {


String str = "<?xml version ="1.0" encoding="UTF-8" standalone="yes"?>" + "<student id ="12">"


  • "<name>nihao</name>" + "<role>" + "<desc>管理</desc>" + "<name>班长</name>" + "</role>" + "</student>";


Student student = JaxbUtil.converyToJavaBean(str, Student.class);


System.out.println(student);


}


}


(4)控制台输出?


4、集合对象的转换(同样适用于 Set)

(1)实体类 1


package com.guor.demo.beanToXml;


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;


import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)


@XmlRootElement(name = "country")


@XmlType(propOrder = { "name", "provinceList" })


public class Country {


@XmlElement(name = "country_name")


private String name;


@XmlElementWrapper(name = "provinces")


@XmlElement(name = "province")


private List<Province> provinceList;


public String getName() {


return name;


}


public void setName(String name) {


this.name = name;


}


public List<Province> getProvinceList() {


return provinceList;


}


public void setProvinceList(List<Province> provinceList) {


this.provinceList = provinceList;

评论

发布
暂无评论
JavaBean与xml互转的方法详解