Java implementation of XML format implementation code _java

Source: Internet
Author: User
Tags string format

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.