Java解析XML文件開源庫(kù)DOM4J
XML解析-DOM4J
DOM4j是一個(gè)開源的,基於java的庫(kù)來(lái)解析XML文檔,它具有高度的靈活性,高性能和內(nèi)存效率的API。DOM4J定義了幾個(gè)java類。以下是最常用的類:
- Document:表示整個(gè)XML文檔。文檔Document對(duì)象是通常被稱為DOM樹。
- Element:表示一個(gè)XML元素。Element對(duì)象有方法來(lái)操作其子元素,文本,屬性和名稱空間。
- Attribute:表示元素的屬性。屬性有方法來(lái)獲取和設(shè)置屬性的值。它有父節(jié)點(diǎn)和屬性類型
- Node:代表元素,屬性或處理指令。
常見DOM4J的方法:
- SAXReader.read(xmlSource):構(gòu)建XML源的DOM4J文檔
- Document.getRootElement():得到的XML的根元素
- Element.node(index):獲得在元素特定索引XML節(jié)點(diǎn)
- Element.attributes():獲得一個(gè)元素的所有屬性
- Node.valueOf(@Name):得到原件的給定名稱的屬性的值
解析DOM4J
使用DOM4J的步驟:
- 導(dǎo)入XML相關(guān)的依賴(dom4j)
- 創(chuàng)建一個(gè)SAXReader
- 從文件或數(shù)據(jù)流創(chuàng)建一個(gè)文檔
- 提取根節(jié)點(diǎn)
private volatile static SystemConfig config = null; public static SystemConfig newInstance() { if (config == null) { synchronized (config) { if (config == null) { config = new SystemConfig(); SAXReader reader = new SAXReader(); try { document = reader.read(configPath); root = document.getRootElement(); } catch (DocumentException e) { e.printStackTrace(); } } } } return config; }
查詢DOM4J
在根節(jié)點(diǎn)root下通過(guò)節(jié)點(diǎn)名稱key查找節(jié)點(diǎn)內(nèi)容value
//獲取節(jié)點(diǎn) root.element(key); //獲取節(jié)點(diǎn)內(nèi)容 root.element(key).getText(); root.element(key).getTextTrim(); root.element(key).getStringValue();
創(chuàng)建DOM4J
在根節(jié)點(diǎn)root下創(chuàng)建節(jié)點(diǎn)
Element element = root.addElement(key); element.setText(value); //將創(chuàng)建的節(jié)點(diǎn)添加進(jìn)編譯後配置文件中 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); try { XMLWriter writer = new XMLWriter(new FileOutputStream(configPath.getPath()), format); writer.write(document); } catch (IOException e) { e.printStackTrace(); }
舉例:
package com.lmc.util; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.util.List; public class SystemConfig { private volatile static SystemConfig config = null; private static URL configPath = SystemConfig.class.getClassLoader().getResource("SystemConfig.xml"); private static Document document; private static Element root; private static final Logger LOGGER = Logger.getLogger(SystemConfig.class); /** * 單例模式,只對(duì)外創(chuàng)建一個(gè)LoadConfig * @return */ public static SystemConfig newInstance() { if (config == null) { synchronized (SystemConfig.class) { if (config == null) { config = new SystemConfig(); SAXReader reader = new SAXReader(); try { document = reader.read(configPath); root = document.getRootElement(); } catch (DocumentException e) { e.printStackTrace(); } } } } return config; } /** * 通過(guò)key返回value * @param key * @return */ public String getValueByKey(String key) { String resultvalue = null; if (root.element(key) == null) { LOGGER.error("The key is not exist!! key: " + key); return null; } else { resultvalue = root.element(key).getStringValue(); if (resultvalue == null) { LOGGER.error("get value by key FAIL!! key: " + key); return null; } } return resultvalue; } /** * 通過(guò)key來(lái)設(shè)置value如果key不存在,創(chuàng)建新的key。 * @param value * @param key */ public void setValueByKey(String value, String key) { Element element = root.addElement(key); element.setText(value); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); try { XMLWriter writer = new XMLWriter(new FileOutputStream(configPath.getPath()), format); writer.write(document); } catch (IOException e) { e.printStackTrace(); } } public void str(String key) { Element element = root.element(key); System.out.println(element.getText()); System.out.println(element.getStringValue()); System.out.println(element.getTextTrim()); } }
到此這篇關(guān)于Java解析XML文件開源庫(kù)DOM4J的文章就介紹到這了,更多相關(guān)Java DOM4J內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security 圖片驗(yàn)證碼功能的實(shí)例代碼
spring security是一系列的過(guò)濾器鏈,所以在這里驗(yàn)證碼也聲明為過(guò)濾器,加在過(guò)濾器鏈的 登錄過(guò)濾器之前,然后自定義一個(gè)異常類,來(lái)響應(yīng)驗(yàn)證碼的錯(cuò)誤信息.這篇文章主要介紹了Spring Security 圖片驗(yàn)證碼,需要的朋友可以參考下2018-03-03利用java監(jiān)聽器實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)
過(guò)去使用ASP和ASP.NET兩種編程的時(shí)候,都寫過(guò)在線人數(shù)統(tǒng)計(jì)能,實(shí)現(xiàn)功能挺簡(jiǎn)單的!今天使用java來(lái)實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)有點(diǎn)另類,是通過(guò)Java監(jiān)聽器實(shí)現(xiàn)的,需要的朋友可以參考下2015-09-09解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09全面了解JAVA_BaseDAO數(shù)據(jù)處理類
下面小編就為大家?guī)?lái)一篇全面了解JAVA_BaseDAO數(shù)據(jù)處理類。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07Spring?Cloud?使用?Resilience4j?實(shí)現(xiàn)服務(wù)熔斷的方法
服務(wù)熔斷是為了保護(hù)我們的服務(wù),比如當(dāng)某個(gè)服務(wù)出現(xiàn)問(wèn)題的時(shí)候,控制打向它的流量,讓它有時(shí)間去恢復(fù),或者限制一段時(shí)間只能有固定數(shù)量的請(qǐng)求打向這個(gè)服務(wù),這篇文章主要介紹了Spring?Cloud?使用?Resilience4j?實(shí)現(xiàn)服務(wù)熔斷,需要的朋友可以參考下2022-12-12mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實(shí)例
這篇文章主要介紹了mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09maven profile實(shí)現(xiàn)多環(huán)境配置的示例
這篇文章主要介紹了maven profile實(shí)現(xiàn)多環(huán)境配置的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01