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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值