mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add xml 2 bean 互转utils;
解决: BeanUtil.beanToMap(outer)只能解析第一层,如果想多层解析,考虑使用JSONUtil的bug; 生产环境,常见是要 解析多层,而不是 只解析第一层;
This commit is contained in:
parent
36dcc17390
commit
96e09601e0
102
hutool-core/src/main/java/cn/hutool/core/util/XmlBeanUtil.java
Normal file
102
hutool-core/src/main/java/cn/hutool/core/util/XmlBeanUtil.java
Normal file
@ -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
|
||||
* 相关介绍:
|
||||
* <ul>
|
||||
* <li><a href=' java实体 和 xml相互转换'>https://www.cnblogs.com/yanghaolie/p/11110991.html</a></li>
|
||||
* <li><a href=' https://my.oschina.net/u/4266515/blog/3330113<'>JAXB "有两个名为 "**" 的属性,类的两个属性具有相同名称 "**""解决方案</a>/li>
|
||||
* </ul>
|
||||
*/
|
||||
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> T convertToJava(String xml, Class<T> 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> T convertToJava(File filePath, Class<T> 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;
|
||||
}
|
||||
}
|
@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
|
||||
"<school>\n" +
|
||||
" <school_name>西安市第一中学</school_name>\n" +
|
||||
" <school_address>西安市雁塔区长安堡一号</school_address>\n" +
|
||||
" <room>\n" +
|
||||
" <room_no>101</room_no>\n" +
|
||||
" <room_name>101教室</room_name>\n" +
|
||||
" </room>\n" +
|
||||
"</school>\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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user