search

XML—SAX for XML parsing

Feb 24, 2017 pm 03:06 PM

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:

XML—SAX for XML parsing

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:

XML—SAX for XML parsing

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 for XML parsing

可以看到,这是对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—SAX for XML parsing

 以上就是XML—XML解析之SAX的内容,更多相关内容请关注PHP中文网(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
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment