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

