Java解析XML文件開源庫DOM4J
XML解析-DOM4J
DOM4j是一個開源的,基於java的庫來解析XML文檔,它具有高度的靈活性,高性能和內(nèi)存效率的API。DOM4J定義了幾個java類。以下是最常用的類:
- Document:表示整個XML文檔。文檔Document對象是通常被稱為DOM樹。
- Element:表示一個XML元素。Element對象有方法來操作其子元素,文本,屬性和名稱空間。
- Attribute:表示元素的屬性。屬性有方法來獲取和設(shè)置屬性的值。它有父節(jié)點和屬性類型
- Node:代表元素,屬性或處理指令。
常見DOM4J的方法:
- SAXReader.read(xmlSource):構(gòu)建XML源的DOM4J文檔
- Document.getRootElement():得到的XML的根元素
- Element.node(index):獲得在元素特定索引XML節(jié)點
- Element.attributes():獲得一個元素的所有屬性
- Node.valueOf(@Name):得到原件的給定名稱的屬性的值
解析DOM4J
使用DOM4J的步驟:
- 導(dǎo)入XML相關(guān)的依賴(dom4j)
- 創(chuàng)建一個SAXReader
- 從文件或數(shù)據(jù)流創(chuàng)建一個文檔
- 提取根節(jié)點
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é)點root下通過節(jié)點名稱key查找節(jié)點內(nèi)容value
//獲取節(jié)點 root.element(key); //獲取節(jié)點內(nèi)容 root.element(key).getText(); root.element(key).getTextTrim(); root.element(key).getStringValue();
創(chuàng)建DOM4J
在根節(jié)點root下創(chuàng)建節(jié)點
Element element = root.addElement(key); element.setText(value); //將創(chuàng)建的節(jié)點添加進編譯後配置文件中 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); /** * 單例模式,只對外創(chuàng)建一個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; } /** * 通過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; } /** * 通過key來設(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文件開源庫DOM4J的文章就介紹到這了,更多相關(guān)Java DOM4J內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計
過去使用ASP和ASP.NET兩種編程的時候,都寫過在線人數(shù)統(tǒng)計能,實現(xiàn)功能挺簡單的!今天使用java來實現(xiàn)在線人數(shù)統(tǒng)計有點另類,是通過Java監(jiān)聽器實現(xiàn)的,需要的朋友可以參考下2015-09-09解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09全面了解JAVA_BaseDAO數(shù)據(jù)處理類
下面小編就為大家?guī)硪黄媪私釰AVA_BaseDAO數(shù)據(jù)處理類。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07Spring?Cloud?使用?Resilience4j?實現(xiàn)服務(wù)熔斷的方法
服務(wù)熔斷是為了保護我們的服務(wù),比如當(dāng)某個服務(wù)出現(xiàn)問題的時候,控制打向它的流量,讓它有時間去恢復(fù),或者限制一段時間只能有固定數(shù)量的請求打向這個服務(wù),這篇文章主要介紹了Spring?Cloud?使用?Resilience4j?實現(xiàn)服務(wù)熔斷,需要的朋友可以參考下2022-12-12mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實例
這篇文章主要介紹了mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09maven profile實現(xiàn)多環(huán)境配置的示例
這篇文章主要介紹了maven profile實現(xiàn)多環(huán)境配置的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01