1. 程式人生 > >DocumentBuilderFactory 解析 XML檔案

DocumentBuilderFactory 解析 XML檔案

先看看xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<configuration config="user.xml">
    <property weburl="www.xzy.com">
        <name>mapreduce.jobhistory.jhist.format</name>
        <value>json</value>
        <source>mapred-default.xml</source>
        <source>job.xml</source>
    </property>
    <property>
        <name>hadoop.proxyuser.hive.groups</name>
        <value>users</value>
        <source>core-site.xml</source>
    </property>
</configuration>

對上訴的XMl分析結構,如果列印根節點root node的子節點,有5個,分別是<root text node>,<first element node>, <first element text node>,<second element node>,<second element text node>;這個結構很重要,很大程度影響程式設計。

這個步驟大體是,建立工廠,生成解析器,得到xml檔案,呼叫parse。coding 如下:

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class docParse {
    public static  void main(String args[]) throws Exception {
        File hostlist = new File("src/main/resources/5.xml");//得到檔案
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();//建立工廠
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(hostlist);//呼叫parse
        doc.getDocumentElement().normalize();
        Element root = doc.getDocumentElement();
        System.out.println("root of xml file:" + root.getTagName());
        NodeList childNodes = root.getChildNodes();
        for (int i = 0;i < childNodes.getLength(); i++){
            if (i == 1)//0是根節點的text node
                System.out.println(childNodes.item(i).getAttributes().getNamedItem("weburl").getNodeValue());// 列印屬性
            System.out.print(childNodes.item(i).getNodeName()); // <root text> <first child> <first child text> <second child> <second child text>
            Node node = childNodes.item(i);
            System.out.print(" type:" + node.getNodeType());
            if (node.getNodeType() == Node.ELEMENT_NODE){
                NodeList firstNods = node.getChildNodes();
                for(int j = 0; j<firstNods.getLength();j++){
                    System.out.print(">>child nods:" + firstNods.item(j).getNodeName());
                    if (firstNods.item(j).getNodeName().equals("name")){
                        System.out.print("--" + firstNods.item(j).getFirstChild().getNodeValue());
                    }
                }
            }
            System.out.println();
        }
    }
}

上訴程式碼只是簡單的測試,只做參考。
XML 檔案的格式可參看  https://www.w3schools.com/xml/xml_tree.asp