From b2f95c92814d6d666af2143fcaa666eedab45fa9 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 9 Aug 2024 09:16:57 +0800 Subject: [PATCH] =?UTF-8?q?XmlUtil=E5=A2=9E=E5=8A=A0xmlToBean=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=EF=BC=8C=E6=94=AF=E6=8C=81CopyOptions=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../java/cn/hutool/core/util/XmlUtil.java | 36 +++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fe036d9a..5160e1007 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.30(2024-08-08) +# 5.8.30(2024-08-09) ### 🐣新特性 * 【core 】 Converter转换规则变更,空对象、空值转为Bean时,创建默认对象,而非null(issue#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) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java index ab8dafea5..1b42ff9a3 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java @@ -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文档写入到文件
* - * @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 xmlToBean(Node node, Class bean) { + return xmlToBean(node, bean, null); + } + + /** + * XML转Java Bean + * + * @param bean类型 + * @param node XML节点 + * @param bean bean类 + * @param copyOptions Bean转换选项,可选是否忽略错误等 + * @return bean + * @see JAXBUtil#xmlToBean(String, Class) + * @since 5.8.30 + */ + public static T xmlToBean(Node node, Class bean, CopyOptions copyOptions) { final Map 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++) {