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;
+ }
+ }
+ }
+}