XmlUtil增加xmlToBean重载,支持CopyOptions参数

This commit is contained in:
Looly 2024-08-09 09:16:57 +08:00
parent 4c00f6adb2
commit b2f95c9281
2 changed files with 28 additions and 11 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.30(2024-08-08)
# 5.8.30(2024-08-09)
### 🐣新特性
* 【core 】 Converter转换规则变更空对象、空值转为Bean时创建默认对象而非nullissue#3649@Github
@ -19,6 +19,7 @@
* 【poi 】 ExcelWriter.autoSizeColumn增加可选widthRatio参数可配置中文字符宽度倍数pr#3689@Github
* 【mail 】 MailAccount增加自定义参数支持issue#3687@Github
* 【mail 】 增加文字颜色与背景颜色色差设置pr#1252@gitee
* 【mail 】 XmlUtil增加xmlToBean重载支持CopyOptions参数issue#IAISBB@gitee
### 🐞Bug修复
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题issue#IAAE88@Gitee

View File

@ -1,6 +1,7 @@
package cn.hutool.core.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
@ -295,12 +296,12 @@ public class XmlUtil {
factory.setNamespaceAware(namespaceAware);
// https://blog.spoock.com/2018/10/23/java-xxe/
try{
try {
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (final Exception ignore){
} catch (final Exception ignore) {
// ignore
}
}
@ -320,11 +321,11 @@ public class XmlUtil {
// https://blog.spoock.com/2018/10/23/java-xxe/
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
// 忽略外部DTD
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
// 不包括外部一般实体
reader.setFeature("http://xml.org/sax/features/external-general-entities",false);
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
// 不包含外部参数实体或外部DTD子集
reader.setFeature("http://xml.org/sax/features/external-parameter-entities",false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
reader.setContentHandler(contentHandler);
reader.parse(source);
@ -487,8 +488,8 @@ public class XmlUtil {
/**
* 将XML文档写入到文件<br>
*
* @param doc XML文档
* @param path 文件路径绝对路径或相对ClassPath路径不存在会自动创建
* @param doc XML文档
* @param path 文件路径绝对路径或相对ClassPath路径不存在会自动创建
* @param charsetName 自定义XML文件的编码如果为{@code null} 读取XML文档中的编码否则默认UTF-8
*/
public static void toFile(Document doc, String path, String charsetName) {
@ -999,6 +1000,21 @@ public class XmlUtil {
* @since 5.2.4
*/
public static <T> T xmlToBean(Node node, Class<T> bean) {
return xmlToBean(node, bean, null);
}
/**
* XML转Java Bean
*
* @param <T> bean类型
* @param node XML节点
* @param bean bean类
* @param copyOptions Bean转换选项可选是否忽略错误等
* @return bean
* @see JAXBUtil#xmlToBean(String, Class)
* @since 5.8.30
*/
public static <T> T xmlToBean(Node node, Class<T> bean, CopyOptions copyOptions) {
final Map<String, Object> map = xmlToMap(node);
if (null != map && map.size() == 1) {
final String simpleName = bean.getSimpleName();
@ -1008,7 +1024,7 @@ public class XmlUtil {
return BeanUtil.toBean(map.get(nodeName), bean);
}
}
return BeanUtil.toBean(map, bean);
return BeanUtil.toBean(map, bean, copyOptions);
}
/**
@ -1264,7 +1280,7 @@ public class XmlUtil {
return null;
}
return mapToXml(BeanUtil.beanToMap(bean, false, ignoreNull),
bean.getClass().getSimpleName(), namespace);
bean.getClass().getSimpleName(), namespace);
}
/**
@ -1487,7 +1503,7 @@ public class XmlUtil {
if (false == attributesOnly) {
final NodeList childNodes = node.getChildNodes();
//noinspection ConstantConditions
if(null != childNodes){
if (null != childNodes) {
Node item;
final int childLength = childNodes.getLength();
for (int i = 0; i < childLength; i++) {