XML(八)-XML的DOM解析
关于节点的getNodeName()和getNodeValue()方法能得到什么值,可以查看Node类的官方文档:
The values of nodeName
, nodeValue
, and attributes
vary according to the node type as follows:
Interface |
nodeName |
nodeValue |
attributes |
|
same as |
same as |
|
|
|
same as |
|
|
|
same as |
|
|
|
|
|
|
|
|
|
|
same as |
|
|
|
same as |
|
|
|
entity name |
|
|
|
name of entity referenced |
|
|
|
notation name |
|
|
|
same as |
same as |
|
|
|
same as |
|
首先是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-8
true
1.0
persons
11
#text
#cdata-section
#text
#comment
#text
person
#text
person
#text
person
#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()-->null
node.getNodeType()-->3 node.getNodeValue()-->
node.getNodeType()-->1 node.getNodeValue()-->null
node.getNodeType()-->3 node.getNodeValue()-->
node.getNodeType()-->1 node.getNodeValue()-->null
node.getNodeType()-->3 node.getNodeValue()-->
===========================================
从今天起,恶心才刚刚开始,
但是这很重要,我不会把不重要的东西教给大家!
这是xml文档的注释
张三
15
南京市
我不是一个随便的人,我随便起来不是人
李小龙
20
美国
张小凡
21
香港