Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of SAX in XML programming

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of SAX in XML programming", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of SAX in XML programming".

XML programming-A basic overview of SAX

SAX, full name Simple API for XML, is not only an interface, but also a software package. It is an alternative to XML parsing. SAX differs from DOM parsing in that it scans the document line by line and parses at the same time. Because the application only checks the data as it is read, there is no need to store the data in memory, which is a huge advantage for parsing large documents.

SAX is a "push" model for dealing with XML event drivers, and although it is not a W3C standard, it is a widely accepted API. Instead of building a complete document tree like DOM, the SAX parser activates a series of events when the document is read, which is pushed to the event handler, which then provides access to the document content.

PS:SAX cannot modify, delete or add XML files.

Why introduce SAX technology?

DOM technology is also a very good DOM parsing solution, why is there SAX technology? The reason is simple: DOM saves XML in the structure of a document tree, which means that XML is read into memory at once, which is not possible in large XML files. That's why SAX, a scanning and parsing technology, came into being.

Schematic diagram

SAX parsing mechanism

SAX parsing allows you to process the document when it is read, without having to wait until the entire document has been loaded.

In Java, you can develop a SAX parser by inheriting the DefaultHandler interface.

The parsing mechanism of SAX is very similar to the event listening mechanism, which waits for an event to trigger and then invokes the corresponding method.

The five most commonly used events for SAX parsers:

1. StartDocument (): this indicates that the SAX parser scans to the beginning of the document.

2. EndDocument (), which indicates where the SAX parser scanned the document.

3. StartElement (), which indicates that the SAX parser scanned the opening tag of an element.

4. Character (), which indicates that the SAX parser scanned some text, and notice that it is stored as an char array.

5. EndElement (), which indicates that the SAX parser scanned the closing tag of an element.

List of commonly used method parameters for event handlers

Public void startDocument ()

Public void startElement (String uri, String localName, String qName,Attributes attributes)

Uri-namespace URI, which is an empty string if the element does not have any namespace URI or is not performing namespace processing.

LocalName-the local name (without a prefix), or an empty string if no namespace processing is being performed.

QName-qualified name (with a prefix), or an empty string if the qualified name is not available.

Attributes-attributes attached to the element. If there is no property, it will be an empty Attributes object.

Public void characters (char [] ch, int start, int length)

Ch-all characters of the document.

Start-the start position in the character array.

Length-the number of characters used from the character array.

Public void endElement (String uri, String localName, String qName)

Uri-namespace URI, which is an empty string if the element does not have any namespace URI or is not performing namespace processing.

LocalName-the local name (without a prefix), or an empty string if no namespace processing is being performed.

QName-qualified name (with a prefix), or an empty string if the qualified name is not available.

Public void endDocument ()

Analytical mode

Using parsers with event handlers, you can parse XML documents. The parser can be created using JAXP's API, and once the SAX parser is created, you can specify the parser to parse an XML document. The event handler is written by the programmer, and the programmer can easily get the data parsed by the sax parser through the parameters of the method in the event handler, thus deciding how to process the data.

Parsing step

1. Get the Sax parser factory object by calling the newInstance () method of SAXParserFactory.

2. Get the parser SAXParser object by calling the newSAXParser () method through the Sax parser factory object.

3. Associate the parser with the event handler object by calling the parser object's parse method

Case study:

XML6.xml

Zhang San 20 is good Li Si 18 is very good Wang Wu 22 is very good Xiao Ming 30. Good package com.pc. Import javax.xml.parsers.*;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class XML6 {/ / use sax technology to parse the xml file public static void main (String [] args) throws Exception, SAXException {/ / TODO Auto-generated method stub / / 1. Create SaxParserFactory SAXParserFactory spf=SAXParserFactory.newInstance (); / / 2. Create the SaxParser parser SAXParser saxParser=spf.newSAXParser (); / 3 associate the xml file with the event handling object saxParser.parse ("src/com/pc/XML6.xml", new MyDefaultHandler2 ());}} / / display only the student's name and age class MyDefaultHandler2 extends DefaultHandler {private boolean isName=false; private boolean isAge=false @ Override public void characters (char [] ch, int start, int length) throws SAXException {/ / TODO Auto-generated method stub String con=new String (ch,start,length); if (! con.trim () .equals (") & & (isName | | isAge)) {System.out.println (con) } isName=false; isAge=false; / / super.characters (ch, start, length);} @ Override public void endDocument () throws SAXException {/ / TODO Auto-generated method stub super.endDocument () @ Override public void endElement (String uri, String localName, String name) throws SAXException {/ / TODO Auto-generated method stub super.endElement (uri, localName, name);} @ Override public void startDocument () throws SAXException {/ / TODO Auto-generated method stub super.startDocument () } @ Override public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException {/ / TODO Auto-generated method stub if (name.equals ("name")) {this.isName=true } else if (name.equals ("age")) {this.isAge=true } / / define event handling class class MyDefaultHandler1 extends DefaultHandler {/ / Discovery document start @ Override public void startDocument () throws SAXException {/ / TODO Auto-generated method stub System.out.println ("startDocument ()"); super.startDocument () } / / found an element @ Override public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException {/ / TODO Auto-generated method stub System.out.println ("element name =" element = "name") in the xml file } / / find the text @ Override public void characters (char [] ch, int start, int length) throws SAXException {String con=new String (ch,start,length) in the xml file / / display text content: if (! con.trim () .equals (")) {System.out.println (new String (ch,start,length)) }} / / find an element in the xml file to introduce @ Override public void endElement (String uri, String localName, String name) throws SAXException {/ / TODO Auto-generated method stub super.endElement (uri, localName, name) } / / find the end of the document @ Override public void endDocument () throws SAXException {/ / TODO Auto-generated method stub System.out.println ("endDocument ()"); super.endDocument ();}} these are all the contents of the article "sample Analysis of SAX in XML programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report