Home  >  Article  >  Backend Development  >  Detailed explanation of SAX parsing process code for XML parsing

Detailed explanation of SAX parsing process code for XML parsing

黄舟
黄舟Original
2017-02-16 15:20:001382browse

Thanks for the parsing principle and process in the previous article. Here we use the code to intuitively understand this principle:

Create a new Demo1 class:


import java.io.File;

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

/**
 * 第一个SAX读取xml文件程序
 * @author APPle
 *
 */
public class Demo1 {

	
	public static void main(String[] args) throws Exception{
		//1.创建SAXParser对象
		//
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		
		//2.调用parse方法
		/**
		 * 参数一: xml文档
		 * 参数二: DefaultHandler的子类,默认基类,因此没法new。用子类
		 */
		parser.parse(new File("./src/contact.xml"), new MyDefaultHandler());
		//使用指定的 DefaultHandler 将指定文件的内容解析为 XML。这里一定要自定义MyDefaultHandler类,且在里面实现业务逻辑。不理解先死记住这种模式。
	}

}


Then the print output of the custom MyDefaultHandler



##

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * SAX处理程序(如何解析xml文档)
 * @author APPle
 *
 */
public class MyDefaultHandler extends DefaultHandler {
	
	/**
	 * 开始文档时调用
	 */
	@Override
	public void startDocument() throws SAXException {
		System.out.println("MyDefaultHandler.startDocument()");
	}
	
	/**
	 * 开始标签时调用
	 * @param qName: 表示开始标签的标签名
	 * @param attributes: 表示开始标签内包含的(属性)【列表】
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		System.out.println("MyDefaultHandler.startElement()-->"+qName);
	}
	
	/**
	 * 结束标签时调用
	 * @param qName: 结束标签的标签名称
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		System.out.println("MyDefaultHandler.endElement()-->"+qName);
	}
	
	/**
	 * 读到文本内容的时调用
	 * @param ch: 表示当前读完的所有文本内容
	 * @param start: 表示当前文本内容的开始位置
	 * @param length: 表示当前文本内容的长度 
	 */ 
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		//得到当前文本内容
		String content = new String(ch,start,length);
		System.out.println("MyDefaultHandler.characters()-->"+content);
	}
	
	/**
	 * 结束文档时调用
	 */
	@Override
	public void endDocument() throws SAXException {
		System.out.println("MyDefaultHandler.endDocument()");
	}
	
}

that inherits the DefaultHandler is given to more intuitively understand the sax parsing process. The next article gives an application case



The above is the detailed explanation of the SAX parsing process code for XML parsing. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn