Android approach to parsing XML based on Pull method _android

Source: Internet
Author: User
Tags sqlite database

The example in this article describes the way Android parses XML based on pull. Share to everyone for your reference, specific as follows:

Pull parsing is similar to sax parsing and is lightweight parsing, embedded in the Android kernel pull, so we don't need to add a third-party jar pack to support pull.

Pull parsing and sax parsing are not the same places:

(1) Pull reads the XML file and triggers the corresponding event call method returns a number

(2) Pull can be in the program to control where you want to resolve to stop parsing.

Take a look at the example:

Book.xml is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<books>
 <book id= "A" >
  <name>thinking In java</name>
  <price>85.5</price>
 </book>
 id= "<book" >
  <name >spring in action</name>
  <price>39.0</price>
 </book>
</books>

pull resolves the following code Pullparseservice.java

Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Org.xmlpull.v1.XmlPullParser;
Import android.util.Xml;
Import Com.xtlh.cn.entity.Book; public class Pullparseservice {public static list<book> Getbooks (InputStream inputstream) throws exception{List
  <Book> books = null;
  Book book = null;
  Xmlpullparser parser = Xml.newpullparser ();
  Parser.setinput (InputStream, "UTF-8"); int event = Parser.geteventtype ()//generates the first event while (event!=xmlpullparser.end_document) {switch (event) {case XMLPULLP Arser.
   START_DOCUMENT://determines whether the current event is a document start event books = new Arraylist<book> ();//initializes the books collection break;  Case xmlpullparser.start_tag://Determine whether the current event is a LABEL element start event if ("book". Equals (Parser.getname ()) {///= Determine if the start Tag element is book book = new
     Book (); Book.setid (Integer.parseint (parser.getattributevalue (0)))//Gets the property value of the book label and sets the ID of the book (book!=null) {if ("Nam E ". Equals (Parser.getname ())) {//Determine if the start tag element is the name Book.setname (Parser.nexttext ()); }else if ("Price". Equals (Parser.getname ()) {//Determine if the start tag element is Price Book.setprice (float.parsefloat (Parser.nexttext ()))
     ;
   }} break; Case xmlpullparser.end_tag://Determine whether the current event is a TAB element end event if ("book". Equals (Parser.getname ()) {/////judge whether the end tag element is book Books.add (bo
    OK)//Add book to book collection book = null;
   } break;
 event = Parser.next ();//Enter the next element and trigger the corresponding event}//end while return book;

 }
}

The Book.java class used inside

public class Book {
 private int id;
 private String name;
 private float price;
 public int getId () {return
  ID;
 }
 public void setId (int id) {
  this.id = ID;
 }
 Public String GetName () {return
  name;
 }
 public void SetName (String name) {
  this.name = name;
 }
 public float GetPrice () {return price
  ;
 }
 public void Setprice (float price) {
  This.price = Price;
 }
 @Override public
 String toString () {return
  this.id+ ":" +this.name+ ":" +this.price
 }
}

The test uses an Android unit test with the following code:

Import Java.io.InputStream;
Import java.util.List;
Import Android.test.AndroidTestCase;
Import Android.util.Log;
Import Com.xtlh.cn.entity.Book;
Import Com.xtlh.cn.service.PullParseService;
public class Testpullparseservice extends androidtestcase{
 private static final String TAG = "Testpullparseservice"; Public
 void Testpull () throws exception{
  InputStream input = This.getclass (). getClassLoader (). getResourceAsStream ("Book.xml");
  Pullparseservice pull = new Pullparseservice ();
  list<book> books = pull.getbooks (input);
  for (book book:books) {
   log.i (tag,book.tostring ());}}}


To use the Android unit test, you need to include a declaration in the Androidmanifest.xml file, androidmanifest.xml as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  package=" Com.xlth.cn.demo "
  android:versioncode=" 1 "
  android:versionname=" 1.0 ">
 < Application android:icon= "@drawable/icon" android:label= "@string/app_name" >
 <uses-library android:name= "Android.test.runner"/>
  <activity android:name=. Pullparsedemo "
     android:label=" @string/app_name ">
   <intent-filter>
    <action android:name= "Android.intent.action.MAIN"/>
    <category android:name= "Android.intent.category.LAUNCHER"/>
   </intent-filter>
  </activity>
 </application>
 <uses-sdk android:minsdkversion = "7"/>
 <instrumentation android:name= "Android.test.InstrumentationTestRunner"
  android: Targetpackage= "Com.xlth.cn.demo" android:label= "Tests for my App"/>
</manifest>

Pull parsing can be used for many occasions, such as the acceptance of Google weather, RSS news, etc.

More interested readers of Android-related content can view this site: "Android operating XML Data Skills Summary", "Android Programming activity Operation Tips Summary", "Android Resource Operation skills Summary", " Android File Operation Tips Summary, "Android operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Introduction and advanced tutorials, overview of the Android View view tips, and a summary of the usage of Android controls

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.