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中文网(www.php.cn)!

Well-formedXMLiscrucialfordataexchangebecauseitensurescorrectparsingandunderstandingacrosssystems.1)Startwithadeclarationlike.2)Ensureeveryopeningtaghasaclosingtagandelementsareproperlynested.3)Useattributescorrectly,enclosingvaluesinquotesandavoidin

XMLisstillusedduetoitsstructurednature,humanreadability,andwidespreadadoptioninenterpriseenvironments.1)Itfacilitatesdataexchangeinsectorslikefinance(SWIFT)andhealthcare(HL7).2)Itshuman-readableformataidsinmanualdatainspectionandediting.3)XMLisusedin

The structure of an RSS document includes three main elements: 1.: root element, defining the RSS version; 2.: Containing channel information, such as title, link, and description; 3.: Representing specific content entries, including title, link, description, etc.

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.

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.

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.

RSS is an XML-based format used to publish frequently updated data. As a web developer, understanding RSS can improve content aggregation and automation update capabilities. By learning RSS structure, parsing and generation methods, you will be able to handle RSSfeeds confidently and optimize your web development skills.

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.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

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