XML parsing - the Dom


First, using the DOM parsing XML Introduction

DOM parser when parsing an XML document, first read the entire XML document, then the whole architecture Dom Document object tree in memory . All the elements will document, in accordance with the hierarchy in which it appears, parsed into one Node objects (nodes).

Pros: The XML files in memory 构造成树形结构,可以遍历和修改节点.

Disadvantages: If the 文件比较大,内存会有压力actual time resolution will be longer.

Second, the step of parsing XML using Dom

(1) parsing a factory object reflector

	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

(2) create a parser object Dom

	DocumentBuilder builder = dbf.newDocumentBuilder();

(3) obtaining Document Document Object

	Document doc = builder.parse("src/com/demo.xml");//此处填写需要解析的XML的路径

It boils down to this: first a reflection analytical factory object, and then create a parser object Dom resolved by this factory object, and then get doc document object by Dom parser object.

Three, Dom parsing XML instance

(1) gives the XML file to parse

<?xml version="1.0" encoding="UTF-8"?>
<employees>
	<employee id="0">
		<name>Alexia</name>
		<age>23</age>
		<sex>Female</sex>
		<weight>150</weight>
		<weight><a>160</a></weight>
	</employee>
	<employee id="1">
		<name height="178">Edward</name>
		<age>24</age>
		<sex>Male</sex>
	</employee>
	<employee id="2">
		<name>wjm</name>
		<age>23</age>
		<sex>Female</sex>
	</employee>
	<employee id="3">
		<name>wh</name>
		<age>24</age>
		<sex>Male</sex>
	</employee>
</employees>

(2) the use of XML parsing Dom

package com.test;

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

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

/**
 * 类说明:
 * 		使用Dom解析XML
 * Dom解析XML的步骤:
 * 		1.创建一个dbf工厂对象的对象.
 * 		2.通过dbf工厂对象创建一个dom解析器DocumentBuilder对象.
 * 		3.通过DocumentBuilder对象的parse(String fileName)方法解析xml文件.
 * 
 * @author qianliangguo
 */
public class DomXML {
	public static void main(String[] args) throws Exception {
		//1.创建[dbf工厂对象]
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//设置解析器忽略注释和空格a 
			dbf.setIgnoringComments(true);
			dbf.setIgnoringElementContentWhitespace(true);
		//2.通过dbf工厂对象获得[Dom解析器对象]
		DocumentBuilder builder = dbf.newDocumentBuilder();
		//3.调用Dom解析器对象解析XML文档,获得[文档Document对象]
		Document doc = builder.parse("src/com/demo.xml");//此处填写需要解析的XML的路径
		
		//--获得根节点
		Element root = doc.getDocumentElement();
			System.out.println(root.getTagName());//获得节点名字
		
		//--根据标签名获得节点集合
		NodeList nodeList = doc.getElementsByTagName("employee");
			System.out.println("employee节点个数:"+nodeList.getLength());
			
			//遍历结点集合
			for(int i=0;i<nodeList.getLength();i++){
				Element element = (Element)nodeList.item(i);
				//获得id属性值
				String attribute = element.getAttribute("id");
				System.out.println("id:"+attribute);
			}
			
			//获得第一个节点下的属性
			Element element = (Element)nodeList.item(0);
			String nameValue = element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
			String ageValue = element.getElementsByTagName("age").item(0).getFirstChild().getNodeValue();
			String sexValue = element.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue();
			String weightValue = element.getElementsByTagName("weight").item(1).getFirstChild().getFirstChild().getNodeValue();
			System.out.println("第一个节点的name文本值:"+nameValue);
			System.out.println("第一个节点的age文本值:"+ageValue);
			System.out.println("第一个节点的sex文本值:"+sexValue);
			System.out.println("第一个节点的第二个weight文本值:"+weightValue);
			
			//获得第二个节点下的属性
			Element element2 = (Element)nodeList.item(1);
			String nameValue2 = element2.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
			String ageValue2 = element2.getElementsByTagName("age").item(0).getFirstChild().getNodeValue();
			String sexValue2 = element2.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue();
			System.out.println("第二个节点的name文本值:"+nameValue2);
			System.out.println("第二个节点的age文本值:"+ageValue2);
			System.out.println("第二个节点的sex文本值:"+sexValue2);
	}
}

Analytical results:
Here Insert Picture Description

Published 332 original articles · won praise 871 · views 130 000 +

Guess you like

Origin blog.csdn.net/weixin_43691058/article/details/103985944