java實現的xml格式化實現代碼_java

來源:互聯網
上載者:User

核心代碼:

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 SAXReader();    // System.out.println(reader);    // 注釋:建立一個串的字元輸入資料流    StringReader in = new StringReader(str);    Document doc = reader.read(in);    // System.out.println(doc.getRootElement());    // 注釋:建立輸出格式    OutputFormat formater = OutputFormat.createPrettyPrint();    //formater=OutputFormat.createCompactFormat();    // 注釋:設定xml的輸出編碼    formater.setEncoding("utf-8");    // 注釋:建立輸出(目標)    StringWriter out = new StringWriter();    // 注釋:建立輸出資料流    XMLWriter writer = new XMLWriter(out, formater);    // 注釋:輸出格式化的串到目標中,執行後。格式化後的串儲存在out中。    writer.write(doc);     writer.close();    System.out.println(out.toString());    // 注釋:返回我們格式化後的結果    return out.toString();  }   public static void main(String[] args) throws Exception {    String head="<?xml version=\"1.0\" encoding=\"GBK\"?>";    String str = "<RequestData><HeadData><UserCode>sh1_admin</UserCode><UserName>sh1_admin</UserName><UserCompanyCode>3107</UserCompanyCode><UserCompanyName>上海分公司一部</UserCompanyName><RequestType>03</RequestType></HeadData><BodyData><ReportId>113100000033</ReportId><Insurant>a5rfg87</Insurant><NumberPlate>滬E78612</NumberPlate><EngineModel></EngineModel><CarVin></CarVin><AccidentDate>2011-02-25 15:07:00</AccidentDate><ReportDate>2011-02-25 15:07:00</ReportDate><Province>310000</Province><City>310100</City><District></District><AccidentPlace>1</AccidentPlace><AccidentLongitude></AccidentLongitude><AccidentLatitude></AccidentLatitude><SurveyLongitude></SurveyLongitude><SurveyLatitude></SurveyLatitude><SceneReportFlag></SceneReportFlag><Reporter></Reporter><ReporterTel></ReporterTel><SurveyPlace></SurveyPlace><OperatorId>3525</OperatorId><OperatorName>sh_admin</OperatorName><ReportDealId>30000800</ReportDealId><ReportDealName>江蘇分公司</ReportDealName><CompanyName></CompanyName><CustomerTypeCode></CustomerTypeCode><ForcePolicyId>a5rfg87a5rfg87a5rfg87</ForcePolicyId><BizPolicyId></BizPolicyId><Index>0</Index><FieldName>5</FieldName></BodyData></RequestData>";    // System.out.println(str);    format(str);  } }

oschina用的線上格式化xml的工具就是使用此段代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.