XML的DOM解析 Java實現 例子二


 

XML的DOM解析 Java實現 例子二

 

  關於節點的getNodeName()getNodeValue()方法能得到什么值,可以查看Node類的官方文檔:

  http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html

  The values of nodeNamenodeValue, and attributes vary according to the node type as follows:

 

Interface

nodeName

nodeValue

attributes

Attr

same as Attr.name

same as Attr.value

null

CDATASection

"#cdata-section"

same as CharacterData.data, the content of the CDATA Section

null

Comment

"#comment"

same as CharacterData.data, the content of the comment

null

Document

"#document"

null

null

DocumentFragment

"#document-fragment"

null

null

DocumentType

same as DocumentType.name

null

null

Element

same as Element.tagName

null

NamedNodeMap

Entity

entity name

null

null

EntityReference

name of entity referenced

null

null

Notation

notation name

null

null

ProcessingInstruction

same as ProcessingInstruction.target

same as ProcessingInstruction.data

null

Text

"#text"

same as CharacterData.data, the content of the text node

null

  See also the Document Object Model (DOM) Level 3 Core Specification.

 

例子程序

  首先是XML文檔如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<學生名冊 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Course30\student.xsd">
    <學生 學號="1">
        <姓名>張三</姓名>
        <性別></性別>
        <年齡>20</年齡>
    </學生>
    <學生 學號="2">
        <姓名>李四</姓名>
        <性別></性別>
        <年齡>19</年齡>
    </學生>
    <學生 學號="3">
        <姓名>王五</姓名>
        <性別></性別>
        <年齡>21</年齡>
    </學生>
</學生名冊>

 

 

  然后是Java程序: 

package com.learnjava.xml.dom;

import java.io.File;

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

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

public class DomTest2
{
    public static void main(String[] args) throws Exception
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(new File("student.xml"));

        // System.out.println(doc.getXmlEncoding());
        // System.out.println(doc.getXmlVersion());
        // System.out.println(doc.getXmlStandalone());

        // 得到根節點
        Element root = doc.getDocumentElement();
        System.out.println("rootTagName: " + root.getTagName());

        // 得到根節點的子節點,注意此處的空格也被計為子節點
        NodeList list = root.getChildNodes();
        System.out.println("root child Count: " + list.getLength());

        for (int i = 0; i < list.getLength(); i++)
        {
            System.out.println("item " + i + ": getNodeName: "
                    + list.item(i).getNodeName());
        }

        System.out.println("------------NodeType and Value----------------");

        for (int i = 0; i < list.getLength(); i++)
        {
            Node n = list.item(i);

            // getNodeType返回的是short型的常量值,文檔中有定義每個值對應的類型
            System.out.println("getNodeType: " + n.getNodeType()
                    + " , getNodeValue: " + n.getNodeValue());
        }

        System.out.println("-----------getTextContent----------------");

        for (int i = 0; i < list.getLength(); i++)
        {
            Node n = list.item(i);

            System.out.println(n.getTextContent());
        }

        System.out.println("-------------屬性測試----------------");

        NodeList nodeList = doc.getElementsByTagName("學生");

        for (int i = 0; i < nodeList.getLength(); i++)
        {
            NamedNodeMap nnm = nodeList.item(i).getAttributes();

            String attrName = nnm.item(0).getNodeName();
            System.out.print("node " + i + ": " + attrName);
            System.out.print("=");

            // 屬性的名字和值即為本身的名字和值
            String attrValue = nnm.item(0).getNodeValue();
            System.out.println(attrValue);
        }

    }
}

 

 

  程序輸出: 

rootTagName: 學生名冊
root child Count: 7
item 0: getNodeName: #text
item 1: getNodeName: 學生
item 2: getNodeName: #text
item 3: getNodeName: 學生
item 4: getNodeName: #text
item 5: getNodeName: 學生
item 6: getNodeName: #text
------------NodeType and Value----------------
getNodeType: 3 , getNodeValue: 
    
getNodeType: 1 , getNodeValue: null
getNodeType: 3 , getNodeValue: 
    
getNodeType: 1 , getNodeValue: null
getNodeType: 3 , getNodeValue: 
    
getNodeType: 1 , getNodeValue: null
getNodeType: 3 , getNodeValue: 

-----------getTextContent----------------

    

        張三
        男
        20
    

    

        李四
        女
        19
    

    

        王五
        男
        21
    


-------------屬性測試----------------
node 0: 學號=1
node 1: 學號=2
node 2: 學號=3

 

 

參考資料

  聖思園張龍老師視頻教程。

  Java官方文檔:http://docs.oracle.com/javase/7/docs/api/index.html

 

 


免責聲明!

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



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