java實現(xiàn)的xml格式化實現(xiàn)代碼
更新時間:2016年11月08日 23:53:57 投稿:mdxy-dxy
這篇文章主要介紹了java實現(xiàn)的xml格式化實現(xiàn)代碼,需要的朋友可以參考下
核心代碼:
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);
// 注釋:創(chuàng)建一個串的字符輸入流
StringReader in = new StringReader(str);
Document doc = reader.read(in);
// System.out.println(doc.getRootElement());
// 注釋:創(chuàng)建輸出格式
OutputFormat formater = OutputFormat.createPrettyPrint();
//formater=OutputFormat.createCompactFormat();
// 注釋:設(shè)置xml的輸出編碼
formater.setEncoding("utf-8");
// 注釋:創(chuàng)建輸出(目標(biāo))
StringWriter out = new StringWriter();
// 注釋:創(chuàng)建輸出流
XMLWriter writer = new XMLWriter(out, formater);
// 注釋:輸出格式化的串到目標(biāo)中,執(zhí)行后。格式化后的串保存在out中。
writer.write(doc);
writer.close();
System.out.println(out.toString());
// 注釋:返回我們格式化后的結(jié)果
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的工具就是使用此段代碼。
相關(guān)文章
Java實現(xiàn)飛機大戰(zhàn)-連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫
這篇文章給大家分享了Java實現(xiàn)飛機大戰(zhàn)中連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫的相關(guān)知識點和代碼,有興趣的可以學(xué)習(xí)參考下。2018-07-07
Mapstruct對象插入數(shù)據(jù)庫某個字段總是為空的bug詳解
這篇文章主要為大家介紹了在一次需求開發(fā)Mapstruct中對象插入數(shù)據(jù)庫某個字段總是為空的bug問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
springboot整合swagger3和knife4j的詳細(xì)過程
knife4j的前身是swagger-bootstrap-ui,取名knife4j是希望她能像一把匕首一樣小巧,輕量,并且功能強悍,下面這篇文章主要介紹了springboot整合swagger3和knife4j的詳細(xì)過程,需要的朋友可以參考下2022-11-11
Java中使用LocalDate根據(jù)日期來計算年齡的實現(xiàn)方法
這篇文章主要介紹了Java中使用LocalDate根據(jù)日期來計算年齡的實現(xiàn)方法,需要的朋友可以參考下2018-01-01

