Core code:
Package com.ddatsh;
Import java.io.IOException;
Import Java.io.StringReader;
Import Java.io.StringWriter;
Import java.io.UnsupportedEncodingException;
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 Xmlformat {public static string format (String str) throws Exception {Saxreader reader = new Saxread
ER ();
SYSTEM.OUT.PRINTLN (reader);
Note: Create a string of character input stream StringReader in = new StringReader (str);
Document doc = Reader.read (in);
System.out.println (Doc.getrootelement ());
Note: Create output format OutputFormat Formater = Outputformat.createprettyprint ();
Formater=outputformat.createcompactformat ();
Note: Sets the output encoding formater.setencoding ("Utf-8") of XML;
Note: Create output (target) StringWriter out = new StringWriter ();
Note: Create output stream XMLWriter writer = new XMLWriter (out, formater); Note: Output formatted string to target, after execution.
The formatted string is saved in out. Writer. write (DOC);
Writer.close ();
System.out.println (Out.tostring ());
Note: Returns the result of our formatted return out.tostring (); public static void Main (string[] args) throws Exception {String head= ' <?xml version=\ ' 1.0\ ' encoding=\ ' gbk\ '
?> "; String str = "<requestdata>
The
Oschina tool for online formatting XML is to use this piece of code.