DOM4j是一個開源的,基於java的庫來解析XML文檔,它具有高度的靈活性,高性能和內存效率的API。DOM4J定義了幾個java類。以下是最常用的類:
常見DOM4J的方法:
使用DOM4J的步驟:
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; }
在根節點root下通過節點名稱key查找節點內容value
//獲取節點 root.element(key); //獲取節點內容 root.element(key).getText(); root.element(key).getTextTrim(); root.element(key).getStringValue();
在根節點root下創建節點
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(); }
舉例:
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); /** * 單例模式,只對外創建一個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來設置value如果key不存在,創建新的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()); } }