Parser工作技能_使用Java Dom對XML格式資料進行parser


於工作中會需要處理各種形式資料
其中一項XML  是最常見的


在如下網站線上編輯XML和立即可視化的網頁中
我們更能清楚瞭解 XML 就是一種有樹狀層次結構的資料
如下方的根節點則是Company
在下一層的子節點則是Employee
以此類推......

那我們又把 Company , Employee  FirstName, LastName ContactNo , Email ....etc
稱為 Tag
視為 Attribute

https://www.tutorialspoint.com/online_xml_editor.htm




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?xml version="1.0"?>
<Company>
    <Employee>
        <FirstName>Tanmay</FirstName>
        <LastName>Patil</LastName>
        <ContactNo>1234567890</ContactNo>
        <Email>tanmaypatil@xyz.com</Email>
        <Address>
            <City>Bangalore</City>
            <State>Karnataka</State>
            <Zip>560212</Zip>
        </Address>
    </Employee>
</Company>









這裡我們用如下這個XML格式的人員資料表進行示範


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?xml version="1.0"?>
<people>
    <person id = "1">
        <lastname>Doe</lastname>
        <firstname>John</firstname>
    </person>
    <person id = "2">
        <lastname>Smith</lastname>
        <firstname>Jim</firstname>
    </person>
</people>


和上面不太相同的就是
people根節點下方兩個person子節點各自都有id這個屬性(用雙引號包覆)對應編號
下去則是各個子節點內部的節點元素(屬性 對應包覆的 文字內容)


額外的  DOM jar檔案可由如下網址下載作專案API添加
http://www.java2s.com/Code/Jar/d/dom.htm
在此使用1.5版本進行開發
http://www.java2s.com/Code/Jar/d/Downloaddommatchers15jar.htm



將dom-matchers-1.5.jar
匯入後
即可import的到


並在專案所在目錄中存放我們建立的people  xml 檔案


程式做完parser後的執行結果

CODE


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javadomapp;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 *
 * @author chous
 */
public class JavaDomApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        DocumentBuilderFactory myFac = DocumentBuilderFactory.newInstance();
        
        DocumentBuilder myBuilder;
        try {
            myBuilder = myFac.newDocumentBuilder();
            Document doc = myBuilder.parse("people.xml");
            NodeList personList = doc.getElementsByTagName("person");
            System.out.println("total person node:" + personList.getLength());
            for(int i=0;i<personList.getLength();i++){
                Node pNode = personList.item(i);
                if(pNode.getNodeType() == Node.ELEMENT_NODE){
                    Element pElement = (Element) pNode;
                    String id = pElement.getAttribute("id");
                    NodeList nameList = pElement.getChildNodes();
                    for(int j=0;j<nameList.getLength();j++){
                        Node n = nameList.item(j);
                        if(n.getNodeType()==Node.ELEMENT_NODE){
                            Element name = (Element) n;
                            System.out.println("Person "+id+":"+name.getTagName()
                                    +"="+name.getTextContent());
                        }
                    }
                }
            }
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
}





留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題