XML(八)-XML的DOM解析
来源:互联网 发布:易语言客户端发送数据 编辑:程序博客网 时间:2024/05/17 04:12
The values of nodeName
, nodeValue
, 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 香港
- XML(八)-XML的DOM解析
- xml的dom解析
- XML的Dom解析
- XML的解析DOM
- XML的DOM解析
- XML的解析 DOM解析
- 【XML】DOM解析XML
- XML DOM---解析xml dom
- XML的DOM解析脚本
- xml dom的简单解析
- DOM的方式解析XML
- xml文件的dom解析
- XML的dom解析详解
- 十九、XML的Dom解析
- Java的Dom解析xml
- 解析xml dom 解析
- xml解析 Dom解析
- 39.XML数据解析的DOM解析
- XML(五)-Schema验证
- XML(六)-DOM解析
- 灵活构建和拼接Laravel的SQL语句
- 面向对象-异常机制
- XML(七)-DOM解析
- XML(八)-XML的DOM解析
- ZCMU-1017: 勇者斗恶龙
- XML(九)-使用DOM来生成xml文档
- XML(十)- SAX解析
- Gradle for mac 安装配置
- Java8学习笔记(一)-初步认识Lambda表达式
- mac下面安装mysql问题
- Java8学习笔记(二)-函数式接口与方法引用
- Java8学习笔记(三)-Lambda表达式深入与流初步