Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Android SAX parser

Shulou Source: shulou.com Published: 2022-06-02 06:07:40 09月10日 Update

This article introduces the relevant knowledge of "how to implement the Android SAX parser". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The de > RssHandlerde > class extends the de > org.xml.sax.helpers.DefaultHandlerde > class. This class provides a default non-operational implementation for all methods corresponding to events generated by the SAX parser. This allows subclasses to override only a few methods as needed. De > RssHandlerde > provides an additional API, de > getMessagesde >. It returns the list of de > Messagede > objects collected by the handler when it receives events from the SAX parser. It has two other internal variables, de > currentMessagede > for the parsed de > Messagede > instance, and the de > StringBuilderde > variable named de > builderde >, which is used to store character data in the text node. The de > startDocumentde > method is called when the parser sends the corresponding event to the handler, and the initialization of these two variables is completed at this point.

Check the de > startElementde > method. It is called every time a start tag is encountered in an XML document. All you care about is when the tag is de > ITEMde > tag. In this case, you will create a new de > Messagede >. Now let's look at the de > charactersde > method. This method is called when character data in a text node is encountered. The data is simply added to the de > builderde > variable. *, let's look at the de > endElementde > method. This method is called when a closing tag is encountered. For tags that correspond to a de > Messagede > attribute, such as de > TITLEde > and de > LINKde >, use the data in the de > builderde > variable to set the appropriate attribute on de > currentMessagede >. If the closing tag is a de > ITEMde >, then de > currentMessagede > will be added to the Messages list. All of these are very typical SAX parsing; nothing here is unique to Android. Therefore, if you know how to write a Java SAX parser, you should know how to write an Android SAX parser. However, android sdk does add some convenient features to SAX.

Android SAX parser

Java Code:

Public class AndroidSaxFeedParser extends BaseFeedParser {public AndroidSaxFeedParser (String feedUrl) {super (feedUrl);} public List

< Message >

Parse () {RssHandler handler = new RssHandler (); try {Xml.parse (this.getInputStream (), Xml.Encoding.UTF_8, handler);} catch (Exception e) {throw new RuntimeException (e);} return handler.getMessages ();}}

Note that this class still uses a standard SAX handler, so you just reuse the de > RssHandlerde > shown. It's nice to be able to reuse SAX handlers, but the code is a little more complex. As you can imagine, if you need to parse a more complex XML document, the processor may bring a variety of bug. For example, look back at the de > endElementde > method. Notice that it checks whether de > currentMessagede > is null before attempting to set the property. Now, look back at the example XML. Notice that there are some de > TITLEde > and de > LINKde > tags outside the de > ITEMde > tag. This is why you use null checking. Otherwise, each de > TITLEde > tag results in a de > NullPointerExceptionde >. Android provides its own unique SAX API, which eliminates the need for you to write your own SAX handlers.

Simplified Android SAX parser

Java Code:

Public class AndroidSaxFeedParser extends BaseFeedParser {public AndroidSaxFeedParser (String feedUrl) {super (feedUrl);} public List

< Message >

Parse () {final Message currentMessage = new Message (); RootElement root = new RootElement ("rss"); final List

< Message >

Messages = new ArrayList

< Message >

(); Element channel = root.getChild ("channel"); Element item = channel.getChild (ITEM); item.setEndElementListener (new EndElementListener () {public void end () {messages.add (currentMessage.copy ());}}); item.getChild (TITLE) .setEndTextElementListener (new EndTextElementListener () {public void end (String body) {currentMessage.setTitle (body);}}); item.getChild (LINK) .setEndTextElementListener (new EndTextElementListener () {public void end (String body) {currentMessage.setLink (body);}}) Item.getChild (DESCRIPTION) .setEndTextElementListener (new EndTextElementListener () {public void end (String body) {currentMessage.setDescription (body);}}); item.getChild (PUB_DATE) .setEndTextElementListener (new EndTextElementListener () {public void end (String body) {currentMessage.setDate (body);}}); try {Xml.parse (this.getInputStream (), Xml.Encoding.UTF_8,root.getContentHandler ());} catch (Exception e) {throw new RuntimeException (e);} return messages This is the end of the content of "how to implement the Android SAX parser". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Tags methods programs processing variables data events code properties complexity two content characters that is cases text documents more this method knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker OPPO Reno Shulou Tech Info MySQL