Xml file generated by string xml
/**
* Convert the xml of the string to the org. w3c. dom. Document Object.
* @ Param xml
* @ Return
*/
Public static Document getDocument (String xml ){
Document document = null;
Try {
DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance ();
DocumentBuilder db = dbf. newDocumentBuilder ();
InputStream is = new ByteArrayInputStream (xml. getBytes ());
Document = db. parse (is );
} Catch (Exception e ){
E. printStackTrace ();
}
Return document;
}
/**
* Write the org. w3c. dom. Document Object to the specified file.
*
* @ Param doc
* @ Param fileName
* @ Throws Exception
*/
Private static void outputXml (Document doc, String fileName ){
Try {
TransformerFactory tf = TransformerFactory. newInstance ();
Transformer transformer = tf. newTransformer ();
DOMSource source = new DOMSource (doc );
Transformer. setOutputProperty (OutputKeys. ENCODING, "UTF-8 ");
Transformer. setOutputProperty (OutputKeys. INDENT, "yes"); // adds line feed indentation, but the default indentation is 0.
Transformer. setOutputProperty ("{http://xml.apache.org/?t=indent-amount", "2"); // set indent to 2
PrintWriter pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (fileName), "UTF-8 ")));
StreamResult result = new StreamResult (pw );
Transformer. transform (source, result );
} Catch (Exception e ){
E. printStackTrace ();
}
}