增加XML字符串格式化
This commit is contained in:
parent
59bc3ceea5
commit
1e46b1079b
@ -1,15 +1,19 @@
|
|||||||
package ink.wgink.util.xml;
|
package ink.wgink.util.xml;
|
||||||
|
|
||||||
|
import ink.wgink.exceptions.base.SystemException;
|
||||||
import ink.wgink.util.ReflectUtil;
|
import ink.wgink.util.ReflectUtil;
|
||||||
import org.apache.commons.collections.KeyValue;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.dom4j.Document;
|
import org.dom4j.Document;
|
||||||
import org.dom4j.DocumentException;
|
import org.dom4j.DocumentException;
|
||||||
|
import org.dom4j.DocumentHelper;
|
||||||
import org.dom4j.Element;
|
import org.dom4j.Element;
|
||||||
|
import org.dom4j.io.OutputFormat;
|
||||||
import org.dom4j.io.SAXReader;
|
import org.dom4j.io.SAXReader;
|
||||||
|
import org.dom4j.io.XMLWriter;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -72,4 +76,30 @@ public class XMLUtil {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化XML字符串
|
||||||
|
*
|
||||||
|
* @param xml
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String formatXmlString(String xml) {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||||
|
Document document = DocumentHelper.parseText(xml);
|
||||||
|
// 格式化输出格式
|
||||||
|
format.setEncoding("UTF-8");
|
||||||
|
// 格式化输出流
|
||||||
|
XMLWriter xmlWriter = new XMLWriter(writer, format);
|
||||||
|
// 将document写入到输出流
|
||||||
|
xmlWriter.write(document);
|
||||||
|
xmlWriter.close();
|
||||||
|
} catch (DocumentException e) {
|
||||||
|
throw new SystemException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new SystemException(e);
|
||||||
|
}
|
||||||
|
return writer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user