1. 程式人生 > >String轉XML

String轉XML

false throws lar span OS ram stat tty buffer

 /**
     * 
     * 格式化String為Xml
     * 
     * @param inputXML
     * @return
     * @throws Exception
     */
    public static String formatXml(String inputXML) throws Exception {
        String xml = null;
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        org.dom4j.Document document 
= reader.read(new StringReader(inputXML)); try { if (document != null) { StringWriter stringWriter = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setNewLineAfterDeclaration(false); writer
= new XMLWriter(stringWriter, format); writer.write(document); writer.flush(); xml = stringWriter.getBuffer().toString(); } } finally { if (writer != null) { try { writer.close(); }
catch (Exception e) { e.printStackTrace(); } } } return xml; }

String轉XML