XML—SAX for XML parsing
1.SAX parsing
When using DOM to parse an XML document, you need to read the entire XML document and build it in memory Document object of the entire DOM tree, so as to operate on the XML document. In this case, if the XML document is particularly large, it will consume a lot of computer memory, and in severe cases may cause memory overflow.
SAX parsing allows the document to be processed when the document is read, without having to wait until the entire document is loaded before operating the document.
Develop a SAX parser by inheriting
DefaultHandler
【 Note】SAX is mainly used for parsing XML documents and cannot modify, delete or add elements.
1.1.SAX parsing mechanism
sax is a push mechanism, you create a Sax parser, the parser will tell you when it finds the content in the XML document (pushing the event to you, somewhat similar to event listening in Java Swing). It is up to the programmer to decide what to do with these findings.
In SAX-based programs, there are five most commonly used SAX events:
1.startDocument()–> tells you that the parser has found the beginning of the document, tells Your parser starts scanning the document
2.endDocument()–> tells you that the parser found the end of the document
3.startElement()–> tells you that the parser has found a start tag. This event tells you the name of the tag, all attribute names and values of the element
4.characters()–> Tells you that the parser found some text, you will get a character array, the offset of the array and a length offset. With these three variables you can get the text found by the parser
5.endElement()–> tells you that the parser found an end tag. This event tells you the name of the element
1.2. SAX parsing example
Still using the XML example used in DOM parsing, as follows:
<?xml version="1.0" encoding="utf-8" standalone="no"?><班级>
<学生 地址="香港">
<名字>周小星</名字>
<年龄>23</年龄>
<介绍>学习刻苦</介绍>
</学生>
<学生 地址="澳门">
<名字>林晓</名字>
<年龄>25</年龄>
<介绍>是一个好学生</介绍>
</学生></班级>[Steps]:
1. Use SAXParserFactory to create a SAX parsing factory
SAXParserFactory spf = SAXParserFactory.newInstance();
2. Get the parser object through the SAX parsing factory
SAXParser sp = spf.newSAXParser();
3. Associate the parsing object with the event handler object
sp.parse("src/myClass.xml",new MyHandler());The MyHandler here needs to be defined by yourself, And it needs to inherit DefaultHandler, and then rewrite the five sax event methods mentioned above in the MyHandler class. Of course, you can also just override what you need.
For example, what I write nowMyHandler is as follows:
class MyHandler extends DefaultHandler{ /**
* 发现文档开始,该函数只会被调用一次
*/
@Override
public void startDocument() throws SAXException {
System.out.println("startDocument");
} /**
* 发现文档结束,该函数只会被调用一次
*/
@Override
public void endDocument() throws SAXException {
System.out.println("endDocument");
} /**
* 发现XML中的一个元素开始,会被反复调用
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("元素名称:"+qName);
} /**
* 发现XML中的一个元素结束,会被反复调用
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
} /**
* 发现XML文件中的文本,会被反复调用
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException { // 显示文本内容
String text = new String(ch,start,length); if(!text.trim().equals("")){
System.out.println(text);
}
}
}The running result is as follows:

As you can see, this is the correct A kind of traversal of XML documents, and all sax can do is traverse.
So, if we now have such a requirement: Only display the names and ages of all students, but not the students' introductions, how to implement it?
We can define two Boolean variables isName and isAge in the MyHandler class, and identify whether it is a name element or age in the startElement method element, if so, get the corresponding text in the characters method, as follows:
1. Define two Boolean variables
private boolean isName = false;private boolean isAge = false;
2. Add a judgment in the startElement method
@Overridepublic void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException { if(qName.equals("名字")){ this.isName = true;
}else if(qName.equals("年龄")){ this.isAge = true;
}
}3. In the characters method, judge whether to obtain the text based on the identifier
@Overridepublic void characters(char[] ch, int start, int length) throws SAXException { // 显示文本内容
String text = new String(ch,start,length); if(!text.trim().equals("")&&(isName||isAge)){
System.out.println(text);
}
isName = false;
isAge = false;
}Finally, remember to reset the two Boolean variables to false.
The running results are as follows:

1.SAX parsing
Using DOM to parse XML documents When doing this, you need to read the entire XML document, build the Document object of the entire DOM tree in memory, and then operate on the XML document. In this case, if the XML document is particularly large, it will consume a lot of computer memory, and in severe cases may cause memory overflow.
SAX parsing allows the document to be processed when the document is read, without having to wait until the entire document is loaded before operating the document.
Develop a SAX parser by inheriting
DefaultHandler
【 Note】SAX is mainly used for parsing XML documents and cannot modify, delete or add elements.
1.1.SAX parsing mechanism
sax is a push mechanism. You create a sax parser. It will tell you when it finds the content in the XML document (pushing the event to you, somewhat similar to event listening in Java Swing). It is up to the programmer to decide what to do with these findings.
In SAX-based programs, there are five most commonly used SAX events:
1.startDocument()–>告诉你解析器发现了文档的开始,告诉你解析器开始扫描文档
2.endDocument()–>告诉你解析器发现了文档结尾
3.startElement()–>告诉你解析器发现了一个起始标签,该事件告诉你标签的名称、该元素所有的属性名和值
4.characters()–>告诉你解析器发现了一些文本,将得到一个字符数组,该数组的偏移量和一个长度偏移量,有这三个变量你可以得到解析器发现的文本
5.endElement()–>告诉你解析器发现了一个结束标签,该事件告诉你元素的名称
1.2.SAX解析实例
依然使用DOM解析中用到的XML例子,如下:
<?xml version="1.0" encoding="utf-8" standalone="no"?><班级>
<学生 地址="香港">
<名字>周小星</名字>
<年龄>23</年龄>
<介绍>学习刻苦</介绍>
</学生>
<学生 地址="澳门">
<名字>林晓</名字>
<年龄>25</年龄>
<介绍>是一个好学生</介绍>
</学生></班级>【步骤】:
1.使用SAXParserFactory创建SAX解析工厂
SAXParserFactory spf = SAXParserFactory.newInstance();
2.通过SAX解析工厂得到解析器对象
SAXParser sp = spf.newSAXParser();
3.将解析对象和事件处理器对象关联
sp.parse("src/myClass.xml",new MyHandler());这里的MyHandler需要自己定义,并且它要继承DefaultHandler,然后在MyHandler类中重写上文提到的5个sax事件方法,当然也可以只重写自己需要的。
比如现在我写的MyHandler如下:
class MyHandler extends DefaultHandler{ /**
* 发现文档开始,该函数只会被调用一次
*/
@Override
public void startDocument() throws SAXException {
System.out.println("startDocument");
} /**
* 发现文档结束,该函数只会被调用一次
*/
@Override
public void endDocument() throws SAXException {
System.out.println("endDocument");
} /**
* 发现XML中的一个元素开始,会被反复调用
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("元素名称:"+qName);
} /**
* 发现XML中的一个元素结束,会被反复调用
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
} /**
* 发现XML文件中的文本,会被反复调用
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException { // 显示文本内容
String text = new String(ch,start,length);
if(!text.trim().equals("")){
System.out.println(text);
}
}
}运行结果如下:

可以看到,这是对XML文档的一种遍历,而sax能够做的也只是遍历了。
那么,如果现在我们有这样一个需求:只显示所有学生的姓名和年龄,不显示学生的介绍,怎么实现呢?
我们可以在MyHandler类中定义两个布尔变量isName和isAge,在startElement方法中标识是否是姓名元素或者年龄元素,如果是的话才在characters方法中获取对应的文本,如下:
1.定义两个布尔变量
private boolean isName = false;private boolean isAge = false;
2.在startElement方法中添加判断
@Overridepublic void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException { if(qName.equals("名字")){
this.isName = true;
}else if(qName.equals("年龄")){ this.isAge = true;
}
}3.在characters方法中根据标识符进行判断是否获取文本
@Overridepublic void characters(char[] ch, int start, int length) throws SAXException { // 显示文本内容
String text = new String(ch,start,length); if(!text.trim().equals("")&&(isName||isAge)){
System.out.println(text);
}
isName = false;
isAge = false;
}最后要记得将两个布尔变量复位成false。
运行结果如下:

以上就是XML—XML解析之SAX的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
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
20443
7
13592
4
JSON vs. XML: Why RSS Chose XML
May 05, 2025 am 12:01 AM
RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.
RSS in XML: Decoding Tags, Attributes, and Structure
Apr 24, 2025 am 12:09 AM
RSS is an XML-based format used to publish and subscribe to content. The XML structure of an RSS file includes a root element, an element, and multiple elements, each representing a content entry. Read and parse RSS files through XML parser, and users can subscribe and get the latest content.
Understanding RSS Documents: A Comprehensive Guide
May 09, 2025 am 12:15 AM
RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.
XML's Advantages in RSS: A Technical Deep Dive
Apr 23, 2025 am 12:02 AM
XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.
Building XML Applications with C : Practical Examples
May 03, 2025 am 12:16 AM
You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.
RSS, XML and the Modern Web: A Content Syndication Deep Dive
May 08, 2025 am 12:14 AM
RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.
XML in C : Handling Complex Data Structures
May 02, 2025 am 12:04 AM
Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.
Beyond Basics: Advanced RSS Features Enabled by XML
May 07, 2025 am 12:12 AM
RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.





