From 96e09601e0c28117240b6a4f68a21349161b53f5 Mon Sep 17 00:00:00 2001 From: duandazhi Date: Thu, 24 Jun 2021 13:05:32 +0800 Subject: [PATCH] =?UTF-8?q?add=20xml=202=20bean=20=E4=BA=92=E8=BD=ACutils;?= =?UTF-8?q?=20=E8=A7=A3=E5=86=B3=EF=BC=9A=20BeanUtil.beanToMap(outer)?= =?UTF-8?q?=E5=8F=AA=E8=83=BD=E8=A7=A3=E6=9E=90=E7=AC=AC=E4=B8=80=E5=B1=82?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=83=B3=E5=A4=9A=E5=B1=82=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=EF=BC=8C=E8=80=83=E8=99=91=E4=BD=BF=E7=94=A8JSONUtil?= =?UTF-8?q?=E7=9A=84bug;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 生产环境,常见是要 解析多层,而不是 只解析第一层; --- .../java/cn/hutool/core/util/XmlBeanUtil.java | 102 +++++++++++++++++ .../cn/hutool/core/util/XmlBeanUtilTest.java | 103 ++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/util/XmlBeanUtil.java create mode 100644 hutool-core/src/test/java/cn/hutool/core/util/XmlBeanUtilTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/util/XmlBeanUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/XmlBeanUtil.java new file mode 100644 index 000000000..3f7c384d0 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/util/XmlBeanUtil.java @@ -0,0 +1,102 @@ +package cn.hutool.core.util; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import java.io.*; +import java.nio.charset.StandardCharsets; + +/** + * xml和Bean互转工具类,不依赖第三方库;需要使用到:javax.xml 包 + * @author dazer + * 相关介绍: + * + */ +public class XmlBeanUtil { + /** + * JavaBean转换成xml + * + * @param obj + * @param encoding eg: utf-8 + * @param format eg: true + * @return + */ + public static String convertToXml(Object obj, String encoding, boolean format) { + String result = null; + StringWriter writer = null; + try { + JAXBContext context = JAXBContext.newInstance(obj.getClass()); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); + marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); + writer = new StringWriter(); + marshaller.marshal(obj, writer); + result = writer.toString(); + } catch (Exception e) { + throw new RuntimeException("convertToXml 错误:" + e.getMessage(), e); + } finally { + if (writer != null){ + try { + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return result; + } + + public static String convertToXml(Object obj) { + return convertToXml(obj, StandardCharsets.UTF_8.toString(), true); + } + + /** + * xml转换成JavaBean + * + * @param xml + * @param c + * @return + */ + @SuppressWarnings("unchecked") + public static T convertToJava(String xml, Class c) { + if (xml == null || "".equals(xml)) + return null; + T t = null; + StringReader reader = null; + try { + JAXBContext context = JAXBContext.newInstance(c); + Unmarshaller unmarshaller = context.createUnmarshaller(); + reader = new StringReader(xml); + t = (T) unmarshaller.unmarshal(reader); + } catch (Exception e) { + throw new RuntimeException("convertToJava1 错误:" + e.getMessage(), e); + } finally { + if (reader != null) + reader.close(); + } + return t; + } + + @SuppressWarnings("unchecked") + public static T convertToJava(File filePath, Class c) throws IOException { + if (!filePath.exists()) + return null; + T t = null; + FileReader reader = null; + try { + JAXBContext context = JAXBContext.newInstance(c); + Unmarshaller unmarshaller = context.createUnmarshaller(); + reader = new FileReader(filePath); + t = (T) unmarshaller.unmarshal(reader); + } catch (Exception e) { + throw new RuntimeException("convertToJava2 错误:" + e.getMessage(), e); + } finally { + if (reader != null) + reader.close(); + } + return t; + } +} diff --git a/hutool-core/src/test/java/cn/hutool/core/util/XmlBeanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/XmlBeanUtilTest.java new file mode 100644 index 000000000..8fdb8cdcb --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/util/XmlBeanUtilTest.java @@ -0,0 +1,103 @@ +package cn.hutool.core.util; + +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.bind.annotation.*; + +/** + * {@link XmlBeanUtil} 工具类 + * + * @author dazer + * 测试 xml 和 bean 互转工具类 + */ +public class XmlBeanUtilTest { + + @Test + public void convertToXmlTest() { + SchoolVo schoolVo = new SchoolVo(); + schoolVo.setSchoolName("西安市第一中学"); + schoolVo.setSchoolAddress("西安市雁塔区长安堡一号"); + + SchoolVo.RoomVo roomVo = new SchoolVo.RoomVo(); + roomVo.setRoomName("101教室"); + roomVo.setRoomNo("101"); + schoolVo.setRoom(roomVo); + + String xmlStr = "\n" + + "\n" + + " 西安市第一中学\n" + + " 西安市雁塔区长安堡一号\n" + + " \n" + + " 101\n" + + " 101教室\n" + + " \n" + + "\n"; + Assert.assertEquals(XmlBeanUtil.convertToXml(schoolVo), xmlStr); + } + + + @XmlRootElement(name = "school") + @XmlAccessorType(XmlAccessType.FIELD) + private static final class SchoolVo { + @XmlElement(name = "school_name", required = true) + private String schoolName; + @XmlElement(name = "school_address", required = true) + private String schoolAddress; + @XmlElement(name = "room", required = true) + private RoomVo room; + + @XmlTransient + public String getSchoolName() { + return schoolName; + } + + public void setSchoolName(String schoolName) { + this.schoolName = schoolName; + } + + @XmlTransient + public String getSchoolAddress() { + return schoolAddress; + } + + public void setSchoolAddress(String schoolAddress) { + this.schoolAddress = schoolAddress; + } + + @XmlTransient + public RoomVo getRoom() { + return room; + } + + public void setRoom(RoomVo room) { + this.room = room; + } + + @XmlAccessorType(XmlAccessType.FIELD) + public static final class RoomVo { + @XmlElement(name = "room_no", required = true) + private String roomNo; + @XmlElement(name = "room_name", required = true) + private String roomName; + + @XmlTransient + public String getRoomNo() { + return roomNo; + } + + public void setRoomNo(String roomNo) { + this.roomNo = roomNo; + } + + @XmlTransient + public String getRoomName() { + return roomName; + } + + public void setRoomName(String roomName) { + this.roomName = roomName; + } + } + } +}