import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class ToolUtrils { /** * 字符串转化为XML串 * * @param str * @return * @throws Exception */ public static String strChangeToXML(String str) { SAXReader saxReader = new SAXReader(); Document document = null; try { document = saxReader.read(new ByteArrayInputStream(str.getBytes())); } catch (DocumentException e) { e.printStackTrace(); } StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UFT-8"); XMLWriter xmlwriter = new XMLWriter(writer, format); try { xmlwriter.write(document); } catch (IOException e) { e.printStackTrace(); } return writer.toString(); } /** * 测试程序文件 * * @param args */ public static void main(String[] args) { String str = "