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

How to implement ​ based on Pull parser

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to realize based on Pull parser". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to realize based on Pull parser" together.

The DOM parser reads all XML documents into memory and then allows you to traverse the XML tree using the DOM API to retrieve the data you need. This is very intuitive code, and in some ways simpler than SAX-based implementations. DOM, however, is usually more memory-intensive because everything is read into memory first. This is a problem for mobile devices running Android, but it works when XML documents are kept small all the time. This could mean that Android developers would think SAX parsing was more common on Android apps and therefore provide additional utilities for it. Android also provides another type of XML parser, which is the pull parser.

XML pull parser

As mentioned earlier, Android does not provide support for the Java StAX API. Android does, however, ship with a pull parser that works similarly to StAX. It allows your application code to fetch events from the parser, as opposed to SAX parsers automatically pushing events into handlers. Listing 10 shows a pull parser implementation of the feed parsing interface.

Implementation of Pull-Based Parser

Java code:

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

< Message >

parse() { List

< Message >

messages = null; XmlPullParser parser = Xml.newPullParser(); try { // auto-detect the encoding from the stream parser.setInput(this.getInputStream(), null); int eventType = parser.getEventType(); Message currentMessage = null; boolean done = false; while (eventType != XmlPullParser.END_DOCUMENT && ! done){ String name = null; switch (eventType){ case XmlPullParser.START_DOCUMENT: messages = new ArrayList

< Message >

(); break; case XmlPullParser.START_TAG: name = parser.getName(); if (name.equalsIgnoreCase(ITEM)){ currentMessage = new Message(); } else if (currentMessage != null){ if (name.equalsIgnoreCase(LINK)){ currentMessage.setLink (parser.nextText()); } else if (name.equalsIgnoreCase(DESCRIPTION)){ currentMessage.setDescription (parser.nextText()); } else if (name.equalsIgnoreCase(PUB_DATE)){ currentMessage.setDate (parser.nextText()); } else if (name.equalsIgnoreCase(TITLE)){ currentMessage.setTitle (parser.nextText()); } } break; Thank you for reading, the above is the content of "how to achieve based on Pull parser", after learning this article, I believe that everyone has a deeper understanding of how to achieve this problem based on Pull parser, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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