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下通過節(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;
}
/**
* 通過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來(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)文章
一文教會(huì)你使用jmap和MAT進(jìn)行堆內(nèi)存溢出分析
本文介紹關(guān)于jmap和MAT的使用來(lái)進(jìn)行堆內(nèi)存溢出分析,因?yàn)檫@個(gè)內(nèi)存溢出是我們手動(dòng)構(gòu)造出來(lái)的,查找比較簡(jiǎn)單,真的到了生產(chǎn)上面需要我們仔細(xì)排除2021-09-09
Java將字符串String轉(zhuǎn)換為整型Int的兩種方式
這篇文章主要介紹了Java如何將字符串String轉(zhuǎn)換為整型Int,在 Java 中要將 String 類型轉(zhuǎn)化為 int 類型時(shí),需要使用 Integer 類中的 parseInt() 方法或者 valueOf() 方法進(jìn)行轉(zhuǎn)換,本文通過實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2023-04-04
Java基礎(chǔ)強(qiáng)化訓(xùn)練輸入錯(cuò)誤即結(jié)束進(jìn)程
本文主要介紹了Java編程的基礎(chǔ)知識(shí)強(qiáng)化應(yīng)用,文中實(shí)例涉及到了許多基礎(chǔ)知識(shí),new對(duì)象,控制臺(tái)輸入,if語(yǔ)句等。很實(shí)用,需要的朋友可以參考下2017-09-09
SpringBoot設(shè)置接口超時(shí)的方法小結(jié)
這篇文章主要介紹了SpringBoot設(shè)置接口超時(shí)的方法小結(jié),包括配置文件,config配置類及相關(guān)示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Springboot與Maven多環(huán)境配置的解決方案
多環(huán)境配置的解決方案有很多,我看到不少項(xiàng)目的多環(huán)境配置都是使用Maven來(lái)實(shí)現(xiàn)的,本文就實(shí)現(xiàn)Springboot與Maven多環(huán)境配置,感興趣的可以了解下2021-06-06

