欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java解析XML文件開源庫DOM4J

 更新時間:2023年01月11日 10:05:18   作者:李奈 - Leemon  
dom4j是一個Java的XML API,是jdom的升級品,用來讀寫XML文件的。dom4j是一個十分優(yōu)秀的JavaXML API,具有性能優(yōu)異、功能強大和極其易使用的特點,它的性能超過sun公司官方的dom技術(shù),同時它也是一個開放源代碼的軟件

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)文章

  • Spring Security 圖片驗證碼功能的實例代碼

    Spring Security 圖片驗證碼功能的實例代碼

    spring security是一系列的過濾器鏈,所以在這里驗證碼也聲明為過濾器,加在過濾器鏈的 登錄過濾器之前,然后自定義一個異常類,來響應(yīng)驗證碼的錯誤信息.這篇文章主要介紹了Spring Security 圖片驗證碼,需要的朋友可以參考下
    2018-03-03
  • 利用java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計

    利用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
  • Java如何獲取當(dāng)前年份、月份和日期字符串

    Java如何獲取當(dāng)前年份、月份和日期字符串

    Java獲取當(dāng)前年份、月份和日期是通過Calendar類的實例對象來獲取的,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • 解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效

    解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效

    這篇文章主要介紹了解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 全面了解JAVA_BaseDAO數(shù)據(jù)處理類

    全面了解JAVA_BaseDAO數(shù)據(jù)處理類

    下面小編就為大家?guī)硪黄媪私釰AVA_BaseDAO數(shù)據(jù)處理類。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • Spring?Cloud?使用?Resilience4j?實現(xiàn)服務(wù)熔斷的方法

    Spring?Cloud?使用?Resilience4j?實現(xiàn)服務(wù)熔斷的方法

    服務(wù)熔斷是為了保護我們的服務(wù),比如當(dāng)某個服務(wù)出現(xiàn)問題的時候,控制打向它的流量,讓它有時間去恢復(fù),或者限制一段時間只能有固定數(shù)量的請求打向這個服務(wù),這篇文章主要介紹了Spring?Cloud?使用?Resilience4j?實現(xiàn)服務(wù)熔斷,需要的朋友可以參考下
    2022-12-12
  • java使用緩沖流復(fù)制文件的方法

    java使用緩沖流復(fù)制文件的方法

    這篇文章主要為大家詳細介紹了java使用緩沖流復(fù)制文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • SpringBoot注入配置文件的3種方法詳解

    SpringBoot注入配置文件的3種方法詳解

    這篇文章主要介紹了SpringBoot注入配置文件的3種方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實例

    mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實例

    這篇文章主要介紹了mybatis Map查詢結(jié)果下劃線轉(zhuǎn)駝峰的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • maven profile實現(xiàn)多環(huán)境配置的示例

    maven profile實現(xiàn)多環(huán)境配置的示例

    這篇文章主要介紹了maven profile實現(xiàn)多環(huán)境配置的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論