***************************************************************PULL解析和SAX解析都是需要Bean的*****************************************************************************
*****************************************Bean************************************************
public class Data1 { private String id; private String cateurl; private String catetitle; private String catecontent; private String cateprice; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCateurl() { return cateurl; } public void setCateurl(String cateurl) { this.cateurl = cateurl; } public String getCatetitle() { return catetitle; } public void setCatetitle(String catetitle) { this.catetitle = catetitle; } public String getCatecontent() { return catecontent; } public void setCatecontent(String catecontent) { this.catecontent = catecontent; } public String getCateprice() { return cateprice; } public void setCateprice(String cateprice) { this.cateprice = cateprice; } public Data1(String id,String cateurl,String catetitle,String catecontent,String cateprice) { super(); this.id = id; this.cateurl = cateurl; this.catetitle = catetitle; this.catecontent = catecontent; this.cateprice = cateprice; } public Data1() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Data1 [id=" + id + ",cateurl=" + cateurl + ",catetitle=" + catetitle + ",catecontent=" + catecontent + ",cateprice=" + cateprice + "]"; } }
import java.util.ArrayList; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView; private ArrayList<Data1> list; private Data1 data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView1); try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); parser.setInput(getAssets().open("data.xml"),"UTF-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String name = parser.getName(); switch (eventType) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Data1>(); break; case XmlPullParser.START_TAG: if (name.equals("cate")) { data = new Data1(); data.setId(parser.getAttributeValue(0)); } else if (name.equals("cateurl")) { data.setCateurl(parser.nextText()); } else if (name.equals("catetitle")) { data.setCatetitle(parser.nextText()); }else if (name.equals("catecontent")) { data.setCatecontent(parser.nextText()); }else if (name.equals("cateprice")) { data.setCateprice(parser.nextText()); } break; case XmlPullParser.END_TAG: if (name.equals("cate")) { list.add(data); } break; default: break; } eventType = parser.next(); } } catch (Exception e) { // TODO: handle exception } for (Data1 data : list) { textView.append(data.toString()+"\n"); } } }********************************************************OK*************************************************************