Android-pull parsing

Source: Internet
Author: User

Package com.example.weekthreepull;

Import java.util.ArrayList;
Import java.util.List;

Import Org.xmlpull.v1.XmlPullParser;

Import Com.example.adapter.MyAdapter;
Import Com.example.dao.BookDao;
Import Com.example.vo.Book;

Import Android.os.Bundle;
Import android.app.Activity;
Import android.util.Xml;
Import Android.view.Menu;
Import Android.widget.ListView;

public class Mainactivity extends Activity {

list<book> list = new arraylist<book> ();
Private book book;
Private BOOKDAO BD;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Get control
ListView LV = (ListView) Findviewbyid (r.id.lv);

BD = new Bookdao (this);

Pull ();

List = Bd.sel ();

Lv.setadapter (New Myadapter (this,list));

}

private void Pull () {
Get parser
Xmlpullparser Pullparser = Xml.newpullparser ();

try {
To add a data source to a parsing class
Pullparser.setinput (Getassets (). Open ("Data.xml"), "Utf-8");

Define a String variable
String tagName = null;

Get label type
int eventtype = Pullparser.geteventtype ();

while (eventtype! = xmlpullparser.end_document) {
Switch (eventtype) {
Case Xmlpullparser.start_document:

Break

Case Xmlpullparser.start_tag:
TagName = Pullparser.getname ();
if ("book". Equals (TagName)) {
Book = new book ();
}
Break

Case Xmlpullparser.text:
if ("Name". Equals (TagName)) {
Book.setbname (Pullparser.gettext ());
}
else if ("Writer". Equals (TagName)) {
Book.setbwriter (Pullparser.gettext ());
}
else if ("Price". Equals (TagName)) {
Book.setbprice (Integer.parseint (Pullparser.gettext ()));
}
Break

Case Xmlpullparser.end_tag:
TagName = Pullparser.getname ();
if ("book". Equals (TagName)) {
Bd.add (book);
book = null;
}
TagName = "";
Break

Case Xmlpullparser.end_document:

Break

Default
Break
}
Get the next label
EventType = Pullparser.next ();
}

} catch (Exception Exception) {
Exception.printstacktrace ();
}
}

}

Android-pull parsing

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.