字符串XML格式化处理(JAVA)
转换前: <Head><name>wangjj</name><age>23</age></Head>
转换后:
<?xml version="1.0" encoding="gb2312"?>
<Head>
<name>wangjj</name>
<age>23</age>
</Head>
代码:
package com.sun.ATest;
import java.io.StringWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class XmlFormat {
//字符串 XML 格式化处理
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String temp = new XmlFormat().formatXml("<Head><name>wangjj</name><age>23</age></Head>");  System.out.println(temp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String formatXml(String str) throws Exception {
Document document = null;
document = DocumentHelper.parseText(str);
// 格式化输出格式
OutputFormat format = atePrettyPrint();
format.setEncoding("gb2312");
StringWriter writer = new StringWriter();
java xml是什么
/
/ 格式化输出流
XMLWriter xmlWriter = new XMLWriter(writer, format);
// 将document写⼊到输出流
xmlWriter.write(document);
xmlWriter.close();
String();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。