Skip to content

Commit

Permalink
fix xml
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Feb 12, 2019
1 parent d6e669e commit e997448
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* 【core】 NumberUtil增加toBytes和toInt方法
* 【core】 XmlUtil增加format方法,支持缩进
* 【http】 SoapRequest增加executeBody方法(issue#IRN6I@Gitee)
* 【core】 调整XmlUtil.toStr方法对编码的逻辑

### Bug修复
* 【core】 修复AnnotationUtil.getAnnotationValue获取对象错误问题(issue#271@Github)
Expand Down
15 changes: 8 additions & 7 deletions hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -246,13 +246,13 @@ public static String toStr(Document doc, boolean isPretty) {
* @since 3.0.9
*/
public static String toStr(Document doc, String charset, boolean isPretty) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final StringWriter writer = StrUtil.getWriter();
try {
write(doc, out, charset, isPretty ? INDENT_DEFAULT : 0);
write(doc, writer, charset, isPretty ? INDENT_DEFAULT : 0);
} catch (Exception e) {
throw new UtilException(e, "Trans xml document to string error!");
}
return out.toString();
return writer.toString();
}

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ public static void toFile(Document doc, String path, String charset) {
BufferedWriter writer = null;
try {
writer = FileUtil.getWriter(path, charset, false);
write(doc, writer, INDENT_DEFAULT);
write(doc, writer, charset, INDENT_DEFAULT);
} finally {
IoUtil.close(writer);
}
Expand All @@ -317,11 +317,12 @@ public static void toFile(Document doc, String path, String charset) {
*
* @param node {@link Node} XML文档节点或文档本身
* @param writer 写出的Writer,Writer决定了输出XML的编码
* @param charset 编码
* @param indent 格式化输出中缩进量,小于1表示不格式化输出
* @since 3.0.9
*/
public static void write(Node node, Writer writer, int indent) {
transform(new DOMSource(node), new StreamResult(writer), null, indent);
public static void write(Node node, Writer writer, String charset, int indent) {
transform(new DOMSource(node), new StreamResult(writer), charset, indent);
}

/**
Expand Down

0 comments on commit e997448

Please sign in to comment.