From 113a9ba719d572c7e4f9a38449ee7cd477705a77 Mon Sep 17 00:00:00 2001 From: peter zhang Date: Fri, 17 Jan 2020 15:18:00 +0800 Subject: [PATCH] support omit xml declaration --- .../java/cn/hutool/core/util/XmlUtil.java | 132 +++++++++++++++++- .../java/cn/hutool/core/util/XmlUtilTest.java | 10 ++ 2 files changed, 140 insertions(+), 2 deletions(-) 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 404cfa9c6..c4bf446ad 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java @@ -5,7 +5,6 @@ import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.lang.Assert; -import cn.hutool.core.lang.Console; import cn.hutool.core.map.MapUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -249,9 +248,24 @@ public class XmlUtil { * @since 3.0.9 */ public static String toStr(Document doc, String charset, boolean isPretty) { + return toStr(doc, charset, isPretty,false); + } + + /** + * 将XML文档转换为String
+ * 字符编码使用XML文档中的编码,获取不到则使用UTF-8 + * + * @param doc XML文档 + * @param charset 编码 + * @param isPretty 是否格式化输出 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @return XML字符串 + * @since 5.1.2 + */ + public static String toStr(Document doc, String charset, boolean isPretty, boolean omitXmlDeclaration) { final StringWriter writer = StrUtil.getWriter(); try { - write(doc, writer, charset, isPretty ? INDENT_DEFAULT : 0); + write(doc, writer, charset, isPretty ? INDENT_DEFAULT : 0, omitXmlDeclaration); } catch (Exception e) { throw new UtilException(e, "Trans xml document to string error!"); } @@ -328,6 +342,20 @@ public class XmlUtil { transform(new DOMSource(node), new StreamResult(writer), charset, indent); } + /** + * 将XML文档写出 + * + * @param node {@link Node} XML文档节点或文档本身 + * @param writer 写出的Writer,Writer决定了输出XML的编码 + * @param charset 编码 + * @param indent 格式化输出中缩进量,小于1表示不格式化输出 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @since 5.1.2 + */ + public static void write(Node node, Writer writer, String charset, int indent, boolean omitXmlDeclaration) { + transform(new DOMSource(node), new StreamResult(writer), charset, indent, omitXmlDeclaration); + } + /** * 将XML文档写出 * @@ -341,6 +369,20 @@ public class XmlUtil { transform(new DOMSource(node), new StreamResult(out), charset, indent); } + /** + * 将XML文档写出 + * + * @param node {@link Node} XML文档节点或文档本身 + * @param out 写出的Writer,Writer决定了输出XML的编码 + * @param charset 编码 + * @param indent 格式化输出中缩进量,小于1表示不格式化输出 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @since 5.1.2 + */ + public static void write(Node node, OutputStream out, String charset, int indent, boolean omitXmlDeclaration) { + transform(new DOMSource(node), new StreamResult(out), charset, indent, omitXmlDeclaration); + } + /** * 将XML文档写出
* 格式化输出逻辑参考:https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java @@ -352,6 +394,21 @@ public class XmlUtil { * @since 4.0.9 */ public static void transform(Source source, Result result, String charset, int indent) { + transform(source, result, charset, indent, false); + } + + /** + * 将XML文档写出
+ * 格式化输出逻辑参考:https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java + * + * @param source 源 + * @param result 目标 + * @param charset 编码 + * @param indent 格式化输出中缩进量,小于1表示不格式化输出 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @since 5.1.2 + */ + public static void transform(Source source, Result result, String charset, int indent,boolean omitXmlDeclaration) { final TransformerFactory factory = TransformerFactory.newInstance(); try { final Transformer xformer = factory.newTransformer(); @@ -362,6 +419,9 @@ public class XmlUtil { if (StrUtil.isNotBlank(charset)) { xformer.setOutputProperty(OutputKeys.ENCODING, charset); } + if (omitXmlDeclaration){ + xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + } xformer.transform(source, result); } catch (Exception e) { throw new UtilException(e, "Trans xml document to string error!"); @@ -778,6 +838,29 @@ public class XmlUtil { return result; } + /** + * 将Map转换为XML格式的字符串 + * + * @param data Map类型数据 + * @return XML格式的字符串 + * @since 5.1.2 + */ + public static String mapToXmlStr(Map data) { + return toStr(mapToXml(data, "xml")); + } + + /** + * 将Map转换为XML格式的字符串 + * + * @param data Map类型数据 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @return XML格式的字符串 + * @since 5.1.2 + */ + public static String mapToXmlStr(Map data,boolean omitXmlDeclaration) { + return toStr(mapToXml(data, "xml"),CharsetUtil.UTF_8,false,omitXmlDeclaration); + } + /** * 将Map转换为XML格式的字符串 * @@ -803,6 +886,51 @@ public class XmlUtil { return toStr(mapToXml(data, rootName, namespace)); } + /** + * 将Map转换为XML格式的字符串 + * + * @param data Map类型数据 + * @param rootName 根节点名 + * @param namespace 命名空间,可以为null + * @param omitXmlDeclaration 是否输出 xml Declaration + * @return XML格式的字符串 + * @since 5.1.2 + */ + public static String mapToXmlStr(Map data, String rootName, String namespace, boolean omitXmlDeclaration) { + return toStr(mapToXml(data, rootName, namespace), CharsetUtil.UTF_8, false, omitXmlDeclaration); + } + + /** + * 将Map转换为XML格式的字符串 + * + * @param data Map类型数据 + * @param rootName 根节点名 + * @param namespace 命名空间,可以为null + * @param isPretty 是否格式化输出 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @return XML格式的字符串 + * @since 5.1.2 + */ + public static String mapToXmlStr(Map data, String rootName, String namespace, boolean isPretty, boolean omitXmlDeclaration) { + return toStr(mapToXml(data, rootName, namespace), CharsetUtil.UTF_8, isPretty); + } + + /** + * 将Map转换为XML格式的字符串 + * + * @param data Map类型数据 + * @param rootName 根节点名 + * @param namespace 命名空间,可以为null + * @param charset 编码 + * @param isPretty 是否格式化输出 + * @param omitXmlDeclaration 是否输出 xml Declaration + * @return XML格式的字符串 + * @since 5.1.2 + */ + public static String mapToXmlStr(Map data, String rootName, String namespace, String charset,boolean isPretty, boolean omitXmlDeclaration) { + return toStr(mapToXml(data, rootName, namespace), charset, isPretty, omitXmlDeclaration); + } + /** * 将Map转换为XML * diff --git a/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java index 6abdaeb57..3b91071c5 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java @@ -124,4 +124,14 @@ public class XmlUtilTest { Document doc = XmlUtil.readXML("test.xml"); Assert.assertNotNull(doc); } + + @Test + public void mapToXmlTestWithOmitXmlDeclaration() { + + Map map = MapBuilder.create(new LinkedHashMap()) + .put("name", "ddatsh") + .build(); + String xml=XmlUtil.mapToXmlStr(map,true); + Assert.assertEquals(xml,"ddatsh"); + } }