XML parsing - Use Sax


First, the use SAX parsing XML Introduction

SAX parsing can when reading the document, the document will be processed without having to wait until the entire document has loaded the fishes document processing.

Advantages: parsing can begin immediately, 速度快,没有内存压力。
Cons:不能对节点做修改。

Second, the use of XML SAX parsing step

Use Sax parsing XML preparations:

1. Inheritance DefaultHandler class: This is the parsing of XML event handler base class

2. The cover statDocument/ endDocument, startElement/ endElementMethod

(1) Analytical factory object reflector Sax

	SAXParserFactory spf = SAXParserFactory.newInstance();

(2) create Sax parser object

	SAXParser saxParse = spf.newSAXParser();

(3) parses the XML file

		File file = new File("src/com/demo.xml");
		saxParse.parse(file, new SaxXML());//第二个参数为类名

Three, Sax 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 Sax

Description: top to bottom, in order to print the XML attribute names of all elements.

package com.test;

import java.io.File;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 类说明:
 * 		使用SAX解析XML
 * 使用Sax解析XML的准备工作:
 * 		1.继承DefaultHandler类:这是解析XML的事件处理基类
 * 		2.覆盖statDocument/endDocument,startElement/endElement方法
 * 
 * Sax解析XML的步骤:
 * 		1.反射一个Sax解析工厂对象
 * 		2.创建Sax解析器对象
 * 		3.解析XML文件
 * 
 * @author qianliangguo
 */
public class SaxXML extends DefaultHandler{
	public static void main(String[] args) throws Exception{
		//1.反射一个Sax解析工厂对象
		SAXParserFactory spf = SAXParserFactory.newInstance();
		//2.创建Sax解析器对象
		SAXParser saxParse = spf.newSAXParser();
		//3.解析XML文件
		File file = new File("src/com/demo.xml");
		saxParse.parse(file, new SaxXML());
	}
	@Override
	public void startDocument() throws SAXException {
		System.out.println("开始解析文档:");	
	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("文档解析结束.");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		System.out.println("开始解析元素"+qName+",");
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		System.out.println(qName+"元素解析结束.");
	}
}

Analytical results:
Here Insert Picture Description
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/103986301