JAVA讀取XML文件並解析獲取元素、屬性值、子元素信息


JAVA讀取XML文件並解析獲取元素、屬性值、子元素信息

 

關鍵字

  XML讀取  InputStream   DocumentBuilderFactory   Element     Node

 

前言

  最近在學習Spring源碼時,碰到讀取XML配置文件的方法,整理下,備忘並和大家分享

 

正文(直接上源碼)

 XML文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">

    <bean name="HelloWorld" class="com.huishe.HelloWord">
        <property name="textone" value="Hello World!"></property>
        <property name="texttwo" value="Hello SUN!"></property>
    </bean>

</beans>

 

 

XMLParse解析源碼

package com.huishe.testOfSpring;

import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class XMLParse {

    public static void main(String[] args) throws Exception {
        //1-獲取XML-IO流
        InputStream xmlInputStream = getXmlInputStream("xml/tinyioc.xml");
        //2-解析XML-IO流 ,獲取Document 對象,以及Document對象 的根節點
        Element rootElement = getRootElementFromIs(xmlInputStream);
        //3~5-從根元素解析得到元素
        parseElementFromRoot(rootElement);

    //控制台輸出:
    //name == HelloWorld
    //className == com.huishe.HelloWord
    //propertyEle: name == textone
    //propertyEle: value == Hello World!
    //propertyEle: name == texttwo
    //propertyEle: value == Hello SUN! }
//1-獲取XML-IO流 private static InputStream getXmlInputStream(String xmlPath){ InputStream inputStream = null; try { //1-把要解析的 XML 文檔轉化為輸入流,以便 DOM 解析器解析它 inputStream= new FileInputStream(xmlPath); } catch (Exception e) { e.printStackTrace(); } return inputStream; } //2-解析XML-IO流 ,獲取Document 對象,以及Document對象 的根節點 private static Element getRootElementFromIs(InputStream inputStream) throws Exception { if(inputStream == null){ return null; } /* * javax.xml.parsers 包中的DocumentBuilderFactory用於創建DOM模式的解析器對象 , * DocumentBuilderFactory是一個抽象工廠類,它不能直接實例化,但該類提供了一個newInstance方法 , * 這個方法會根據本地平台默認安裝的解析器,自動創建一個工廠的對象並返回。 */ //2-調用 DocumentBuilderFactory.newInstance() 方法得到創建 DOM 解析器的工廠 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //3-調用工廠對象的 newDocumentBuilder方法得到 DOM 解析器對象。 DocumentBuilder docBuilder = factory.newDocumentBuilder(); //4-調用 DOM 解析器對象的 parse() 方法解析 XML 文檔,得到代表整個文檔的 Document 對象,進行可以利用DOM特性對整個XML文檔進行操作了。 Document doc = docBuilder.parse(inputStream); //5-得到 XML 文檔的根節點 Element root =doc.getDocumentElement(); //6-關閉流 if(inputStream != null){ inputStream.close(); } return root; } //3-從根元素解析得到元素 private static void parseElementFromRoot(Element root) { NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element ele = (Element) node; //4-從元素解析得到屬性值 getDataFromElement(ele); //5-從元素解析特定子元素並解析(以property為例) getCertainElementFromParentElement(ele); } } } //4-從元素解析得到屬性值 private static void getDataFromElement(Element ele) { String name = ele.getAttribute("name");//根據屬性名稱讀取屬性值 System.out.println("name == " + name); String className = ele.getAttribute("class"); System.out.println("className == " + className); } //5-從元素解析特定子元素並解析(以property為例) private static void getCertainElementFromParentElement(Element ele) { NodeList propertyEleList = ele.getElementsByTagName("property");//根據標簽名稱獲取標簽元素列表 for (int i = 0; i < propertyEleList.getLength(); i++) { Node node = propertyEleList.item(i); if (node instanceof Element) { Element propertyEle = (Element) node; String name = propertyEle.getAttribute("name"); System.out.println("propertyEle: name == " + name); String value = propertyEle.getAttribute("value"); System.out.println("propertyEle: value == " + value); } } } }
 

 

 

總結

  讀取XML配置涉及到IO、DocumentBuilderFactory、Node等概念,這里只使用,不具體分析

 

 

參考文獻

1- https://blog.csdn.net/hua1017177499/article/details/78985166

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM