XML(八)-XML的DOM解析

来源:互联网 发布:易语言客户端发送数据 编辑:程序博客网 时间:2024/05/17 04:12
关于节点的getNodeName()getNodeValue()方法能得到什么值,可以查看Node类的官方文档:

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

首先是XML文档如下:

<?xml version="1.0"  encoding="UTF-8" standalone="yes"?><!DOCTYPE persons [<!ENTITY xx "我不是一个随便的人,我随便起来不是人">]><persons>    <![CDATA[        从今天起,恶心才刚刚开始,        但是这很重要,我不会把不重要的东西教给大家!    ]]>    <!-- 这是xml文档的注释 -->    <person id = "p01">        <name>张三</name>        <age>15</age>        <address>南京市</address>        <info>&xx;</info>    </person>    <person id = "p02">        <name>李小龙</name>        <age>20</age>        <address>美国</address>    </person>    <person id = "p03">        <name>张小凡</name>        <age>21</age>        <address>香港</address>    </person></persons>

java代码解析

package cn.org.kingdom.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.Node;import org.w3c.dom.NodeList;public class DomParseTest2 {    public static void main(String args[]) throws Exception {        //step1:获得document解析器工厂        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        //step2:获得document解析器        DocumentBuilder db = dbf.newDocumentBuilder();        //step3:通过document解析器得到文档对象        Document doc = db.parse(new File("src/persons.xml"));        //获取xml文档的编码方式        System.out.println("xml文档的编码方式:"+doc.getXmlEncoding());        //是否独立运行        System.out.println(doc.getXmlStandalone());        //获取xml文档的版本号        System.out.println(doc.getXmlVersion());        //获得文档的根元素节点        Element root = doc.getDocumentElement();        //得到根节点的标签名        System.out.println(root.getTagName());        //得到根节点的所有子节点(空格也算是节点的孩子的组成部分(解析时一定要注意))        NodeList nl = root.getChildNodes();        System.out.println(nl.getLength());//返回11个        for(int i = 0;i<nl.getLength();i++) {            System.out.println(nl.item(i).getNodeName());        }        System.out.println("==========================================");        for(int i = 0 ;i<nl.getLength();i++) {            Node node = nl.item(i);            System.out.println("node.getNodeType()-->"+node.getNodeType()+"\tnode.getNodeValue()-->"+node.getNodeValue());            //应该按照此方式进行判断,分别处理            if(node.getNodeType()==Node.ELEMENT_NODE) {                //元素节点            }else if(node.getNodeType()==node.TEXT_NODE){                //文本节点            }        }        System.out.println("===========================================");        for(int i = 0;i<nl.getLength();i++) {            Node node = nl.item(i);            System.out.print(node.getTextContent());        }    }}  

程序输出如下

xml文档的编码方式:UTF-8true1.0persons11#text#cdata-section#text#comment#textperson#textperson#textperson#text==========================================node.getNodeType()-->3    node.getNodeValue()-->    node.getNodeType()-->4    node.getNodeValue()-->        从今天起,恶心才刚刚开始,        但是这很重要,我不会把不重要的东西教给大家!    node.getNodeType()-->3    node.getNodeValue()-->    node.getNodeType()-->8    node.getNodeValue()--> 这是xml文档的注释 node.getNodeType()-->3    node.getNodeValue()-->    node.getNodeType()-->1    node.getNodeValue()-->nullnode.getNodeType()-->3    node.getNodeValue()-->    node.getNodeType()-->1    node.getNodeValue()-->nullnode.getNodeType()-->3    node.getNodeValue()-->    node.getNodeType()-->1    node.getNodeValue()-->nullnode.getNodeType()-->3    node.getNodeValue()-->===========================================            从今天起,恶心才刚刚开始,        但是这很重要,我不会把不重要的东西教给大家!         这是xml文档的注释             张三        15        南京市        我不是一个随便的人,我随便起来不是人                李小龙        20        美国                张小凡        21        香港    

 

原创粉丝点击