


Detailed explanation of SAX parsing process code for XML parsing
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()"); } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In the field of web development, XML and JSON, one of the data formats, are widely used, and the Gin framework is a lightweight Go language web framework that is simple, easy to use and has efficient performance. This article will introduce how to use the Gin framework to implement XML and JSON data parsing functions. Gin Framework Overview The Gin framework is a web framework based on the Go language, which can be used to build efficient and scalable web applications. The Gin framework is designed to be simple and easy to use. It provides a variety of middleware and plug-ins to make the development

As Java becomes more and more widely used in the Internet field, many developers may encounter the problem of "XML parsing error" when using XML for data parsing. XML parsing error means that when using Java to parse XML data, the program cannot parse the data normally due to incorrect data format, unclosed tags, or other reasons, thus causing errors and exceptions. So, how should we solve and avoid when facing XML parsing errors? This article will explain this issue in detail. 1. XML parsing

XML is a commonly used data exchange format. In Java development, large-scale XML files often need to be parsed. However, since XML files often contain a large number of nodes and elements, traditional XML parsing methods can easily lead to high memory usage. This article will introduce some methods to solve the problem of high memory usage of XML parsing. Using the SAX parser SAX (SimpleAPI for XML) is an event-driven XML parsing method. Compared to DOM (DocumentO

When developing web applications, XML is a very important data format that can be used in scenarios such as data exchange and information sharing. In PHP, we can use built-in functions and third-party libraries to parse and manipulate XML. Below we will discuss how to use PHP to parse XML and obtain the content of the nodes in it. Parse XML files First, we need to parse XML files. PHP provides two main methods to parse XML: 1.1. Using SimpleXML SimpleXML is within PHP

PHP8.1 update: Enhanced XML parsing function With the rapid development of the Internet, XML (Extensible Markup Language) plays an important role in data exchange and information transmission. As a universal data format, XML is often used to transfer and store data between different applications. In order to provide better XML parsing capabilities, PHP8.1 has enhanced the XML parsing function to provide developers with more convenience. In PHP8.1, an important improvement is the introduction of libxml_disabl

Solution introduction to solving Java XML parsing exceptions (XMLParsingException): When processing XML files, we often encounter XML parsing exceptions (XMLParsingException). This is caused by XML file format errors or incorrect XML parser configuration. This article will introduce some common XML parsing exceptions and solutions to help developers better deal with these problems. 1. The cause of XML parsing exception is parsing XML document.

PHP is a widely used programming language that supports many different file formats, including XML. When processing XML files, parsing errors may occur. This article will explain how to handle XML parsing errors in PHP and provide some concrete code examples. Checking XML File Format Before processing an XML file, you must ensure that the XML file is in the correct format. The XML file must be in a strict format, otherwise the parser will not be able to process the file. For example, the XML file must contain the root element and use the correct namespace,

How to solve the problem of XML parsing occupying too much heap memory in Java development Introduction: With the explosive growth of information and data, the importance of XML (Extensible Markup Language) in enterprise application development continues to increase. However, you may encounter problems with excessive heap memory usage during XML parsing, especially when dealing with large XML files. This article will introduce some methods and techniques to solve this problem. 1. Understand the XML parsing process. Before we deeply solve the problem of XML parsing occupying too much heap memory, we first understand the basics of XML parsing.
