Article From:

JAVARead the XML file and parse to get element, attribute value, sub-element information



  XMLRead InputStream Document Builder Factory Element Node



  Recently, when learning Spring source code, I came across the method of reading the XML configuration file, sorting it out, forgetting and sharing it with you.


Text (direct source)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="">

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




XMLParseParsing source code

package com.huishe.testOfSpring;


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-Get the XML-IO stream
        InputStream xmlInputStream = getXmlInputStream("xml/tinyioc.xml");
        //2-Parse the XML-IO stream, get the Document object, and the root node of the Document object
        Element rootElement = getRootElementFromIs(xmlInputStream);
        //3~5-Resolve from the root element to get the element

    //Console output:
    //name == HelloWorld
    //className == com.huishe.HelloWord
    //propertyEle: name == textone
    //propertyEle: value == Hello World!
    //propertyEle: name == texttwo
    //propertyEle: value == Hello SUN! }
//1-Get the XML-IO stream private static InputStream getXmlInputStream(String xmlPath){ InputStream inputStream = null; try { //1-Convert the XML document to be parsed into an input stream so that the DOM parser can parse it inputStream= new FileInputStream(xmlPath); } catch (Exception e) { e.printStackTrace(); } return inputStream; } //2-Parse the XML-IO stream, get the Document object, and the root node of the Document object private static Element getRootElementFromIs(InputStream inputStream) throws Exception { if(inputStream == null){ return null; } /* * javax.xml.parsers The Document Builder Factory in the package is used to create parser objects for DOM schemas.* DocumentBuilderFactory is an abstract factory class that cannot be instantiated directly, but it providesA new Instance method,* This method automatically creates a factory object and returns it based on the parser installed by default on the local platform.*/ //2-Call the DocumentBuilderFactory. newInstance () method to get the factory that created the DOM parser DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //3-Call the newDocumentBuilder method of the factory object to get the DOM parser object. DocumentBuilder docBuilder = factory.newDocumentBuilder(); //4-The parse () method of the DOM parser object is called to parse the XML document, and the Document object representing the whole document is obtained. The whole XML document can be manipulated by using the DOM characteristics. Document doc = docBuilder.parse(inputStream); //5-Get the root node of the XML document Element root =doc.getDocumentElement(); //6-Closed flow if(inputStream != null){ inputStream.close(); } return root; } //3-Resolve from the root element to get the element 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-Getting attribute values from element parsing getDataFromElement(ele); //5-Parse specific child elements from elements and parse them (take property as an example) getCertainElementFromParentElement(ele); } } } //4-Getting attribute values from element parsing private static void getDataFromElement(Element ele) { String name = ele.getAttribute("name");//Read attribute values based on attribute names System.out.println("name == " + name); String className = ele.getAttribute("class"); System.out.println("className == " + className); } //5-Parse specific child elements from elements and parse them (take property as an example) private static void getCertainElementFromParentElement(Element ele) { NodeList propertyEleList = ele.getElementsByTagName("property");//Get a list of tag elements based on the tag name 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); } } } }




  Reading XML configuration involves concepts such as IO, Document Builder Factory, Node, etc., which are only used here without specific analysis.






Leave a Reply

Your email address will not be published. Required fields are marked *