您好,登錄后才能下訂單哦!
這篇文章主要介紹SpringBoot框架中管理Xml和CSV的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
XML是可擴(kuò)展標(biāo)記語(yǔ)言,是一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)性的標(biāo)記語(yǔ)言。標(biāo)記指計(jì)算機(jī)所能理解的信息符號(hào),通過(guò)此種標(biāo)記,計(jì)算機(jī)之間可以處理包含各種的信息比如數(shù)據(jù)結(jié)構(gòu),格式等。它可以用來(lái)標(biāo)記數(shù)據(jù)、定義數(shù)據(jù)類型,是一種允許用戶對(duì)自己的標(biāo)記語(yǔ)言進(jìn)行定義的源語(yǔ)言。適合網(wǎng)絡(luò)傳輸,提供統(tǒng)一的方法來(lái)描述和交換應(yīng)用程序的結(jié)構(gòu)化數(shù)據(jù)。
CSV文檔,以逗號(hào)分隔文檔內(nèi)容值,其文件以純文本形式存儲(chǔ)結(jié)構(gòu)數(shù)據(jù)。CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見(jiàn)的是逗號(hào)。CSV是一種通用的、相對(duì)簡(jiǎn)單的文件格式,通常被用在大數(shù)據(jù)領(lǐng)域,進(jìn)行大規(guī)模的數(shù)據(jù)搬運(yùn)操作。
Dom4j是基于Java編寫(xiě)的XML文件操作的API包,用來(lái)讀寫(xiě)XML文件。具有性能優(yōu)異、功能強(qiáng)大和簡(jiǎn)單易使用的特點(diǎn)。
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
涉及對(duì)XML文件讀取、加載、遍歷、創(chuàng)建、修改、刪除等常用方法。
public class XmlUtil { /** * 創(chuàng)建文檔 */ public static Document getDocument (String filename) { File xmlFile = new File(filename) ; Document document = null; if (xmlFile.exists()){ try{ SAXReader saxReader = new SAXReader(); document = saxReader.read(xmlFile); } catch (Exception e){ e.printStackTrace(); } } return document ; } /** * 遍歷根節(jié)點(diǎn) */ public static Document iteratorNode (String filename) { Document document = getDocument(filename) ; if (document != null) { Element root = document.getRootElement(); Iterator iterator = root.elementIterator() ; while (iterator.hasNext()) { Element element = (Element) iterator.next(); System.out.println(element.getName()); } } return document ; } /** * 創(chuàng)建XML文檔 */ public static void createXML (String filePath) throws Exception { // 創(chuàng)建 Document 對(duì)象 Document document = DocumentHelper.createDocument(); // 創(chuàng)建節(jié)點(diǎn),首個(gè)節(jié)點(diǎn)默認(rèn)為根節(jié)點(diǎn) Element rootElement = document.addElement("project"); Element parentElement = rootElement.addElement("parent"); parentElement.addComment("版本描述") ; Element groupIdElement = parentElement.addElement("groupId") ; Element artifactIdElement = parentElement.addElement("artifactId") ; Element versionElement = parentElement.addElement("version") ; groupIdElement.setText("SpringBoot2"); artifactIdElement.setText("spring-boot-starters"); versionElement.setText("2.1.3.RELEASE"); //設(shè)置輸出編碼 OutputFormat format = OutputFormat.createPrettyPrint(); File xmlFile = new File(filePath); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile),format); writer.write(document); writer.close(); } /** * 更新節(jié)點(diǎn) */ public static void updateXML (String filePath) throws Exception { Document document = getDocument (filePath) ; if (document != null){ // 修改指定節(jié)點(diǎn) List elementList = document.selectNodes("/project/parent/groupId"); Iterator iterator = elementList.iterator() ; while (iterator.hasNext()){ Element element = (Element) iterator.next() ; element.setText("spring-boot-2"); } //設(shè)置輸出編碼 OutputFormat format = OutputFormat.createPrettyPrint(); File xmlFile = new File(filePath); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile),format); writer.write(document); writer.close(); } } /** * 刪除節(jié)點(diǎn) */ public static void removeElement (String filePath) throws Exception { Document document = getDocument (filePath) ; if (document != null){ // 修改指定節(jié)點(diǎn) List elementList = document.selectNodes("/project/parent"); Iterator iterator = elementList.iterator() ; while (iterator.hasNext()){ Element parentElement = (Element) iterator.next() ; Iterator parentIterator = parentElement.elementIterator() ; while (parentIterator.hasNext()){ Element childElement = (Element)parentIterator.next() ; if (childElement.getName().equals("version")) { parentElement.remove(childElement) ; } } } //設(shè)置輸出編碼 OutputFormat format = OutputFormat.createPrettyPrint(); File xmlFile = new File(filePath); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile),format); writer.write(document); writer.close(); } } public static void main(String[] args) throws Exception { String filePath = "F:\\file-type\\project-cf.xml" ; // 1、創(chuàng)建文檔 Document document = getDocument(filePath) ; System.out.println(document.getRootElement().getName()); // 2、根節(jié)點(diǎn)遍歷 iteratorNode(filePath); // 3、創(chuàng)建XML文件 String newFile = "F:\\file-type\\project-cf-new.xml" ; createXML(newFile) ; // 4、更新XML文件 updateXML(newFile) ; // 5、刪除節(jié)點(diǎn) removeElement(newFile) ; } }
這里不需要依賴特定的Jar包,按照普通的文件讀取即可。
@Async @Override public void readNotify(String path, Integer columnSize) throws Exception { File file = new File(path) ; String fileName = file.getName() ; int lineNum = 0 ; if (fileName.startsWith("data-")) { InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"GBK") ; BufferedReader reader = new BufferedReader(isr); List<DataInfo> dataInfoList = new ArrayList<>(4); String line ; while ((line = reader.readLine()) != null) { lineNum ++ ; String[] dataArray = line.split(","); if (dataArray.length == columnSize) { String cityName = new String(dataArray[1].getBytes(),"UTF-8") ; dataInfoList.add(new DataInfo(Integer.parseInt(dataArray[0]),cityName,dataArray[2])) ; } if (dataInfoList.size() >= 4){ LOGGER.info("容器數(shù)據(jù):"+dataInfoList); dataInfoList.clear(); } } if (dataInfoList.size()>0){ LOGGER.info("最后數(shù)據(jù):"+dataInfoList); } reader.close(); } LOGGER.info("讀取數(shù)據(jù)條數(shù):"+lineNum); }
@Async @Override public void createCsv(List<String> dataList,String path) throws Exception { File file = new File(path) ; boolean createFile = false ; if (file.exists()){ boolean deleteFile = file.delete() ; LOGGER.info("deleteFile:"+deleteFile); } createFile = file.createNewFile() ; OutputStreamWriter ost = new OutputStreamWriter(new FileOutputStream(path),"UTF-8") ; BufferedWriter out = new BufferedWriter(ost); if (createFile){ for (String line:dataList){ if (!StringUtils.isEmpty(line)){ out.write(line); out.newLine(); } } } out.close(); }
這里基于Swagger2管理接口測(cè)試 。
@Api("Csv接口管理") @RestController public class CsvWeb { @Resource private CsvService csvService ; @ApiOperation(value="文件讀取") @GetMapping("/csv/readNotify") public String readNotify (@RequestParam("path") String path, @RequestParam("column") Integer columnSize) throws Exception { csvService.readNotify(path,columnSize); return "success" ; } @ApiOperation(value="創(chuàng)建文件") @GetMapping("/csv/createCsv") public String createCsv (@RequestParam("path") String path) throws Exception { List<String> dataList = new ArrayList<>() ; dataList.add("1,北京,beijing") ; dataList.add("2,上海,shanghai") ; dataList.add("3,蘇州,suzhou") ; csvService.createCsv(dataList,path); return "success" ; } }
以上是“SpringBoot框架中管理Xml和CSV的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。