xml一共有三种解析放方法
①DOM解析
②SAX解析
③Android内置的Pull解析
第三种 解析
③Android内置的Pull解析
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import org.xmlpull.v1.XmlPullParser;
- import com.example.adapter.MyAdapter;
- import com.example.bean.News;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.res.XmlResourceParser;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.util.Xml;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- public class MainActivity extends Activity {
- private ListView lv;
- private List<News> list;
- private BaseAdapter adapter;
- Handler handler=new Handler(){
- public void handleMessage(android.os.Message msg) {
- handler.sendEmptyMessageDelayed(0, 1000);
- adapter=new MyAdapter(MainActivity.this, list);
- lv.setAdapter(adapter);
-
- };
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lv = (ListView) findViewById(R.id.lv);
-
-
- new Thread(){
- public void run() {
- handler.sendEmptyMessageDelayed(0, 1000);
- show("http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20");
- }
-
- }.start();
- //点击条目跳转
- lv.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- String link=list.get(arg2).getLink();
-
- Intent intent=new Intent(MainActivity.this, SecondActivity.class);
- intent.putExtra("link", link);
- startActivity(intent);
-
- }
- });
-
- }
- //解析数据
- private void show(String strUrl) {
- try {
- URL url=new URL(strUrl);
- //创建连接
- HttpURLConnection connection=(HttpURLConnection) url.openConnection();
- connection.connect();
- //获得响应码
- int code = connection.getResponseCode();
- if(code==200){
- InputStream is = connection.getInputStream();
- //pull解析
- XmlPullParser xmlParser=Xml.newPullParser();
- xmlParser.setInput(is, "UTF-8");
- News n=null;
- int eventType = xmlParser.getEventType();
- while(eventType!=XmlResourceParser.END_DOCUMENT){
- String tagName = xmlParser.getName();
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:
- list = new ArrayList<News>();
- break;
- case XmlPullParser.START_TAG:
- //如果新闻标签的开始我们需要创建News对象
- if("item".equalsIgnoreCase(tagName)){
- n=new News();
- }else if("title".equals(tagName)){
- n.setTitle(xmlParser.nextText());
- }else if("link".equals(tagName)){
- n.setLink(xmlParser.nextText());
- }else if("imgs".equals(tagName)){
- n.setImgs(xmlParser.nextText());
- }else if("copyright".equals(tagName)){
- n.setCopyright(xmlParser.nextText());
- }else if("pubDate".equals(tagName)){
- n.setPubDate(xmlParser.nextText());
- }
- break;
- case XmlPullParser.END_TAG:
- if("item".equals(tagName)){
- list.add(n);
- Log.e("info", "++++++"+n);
- n=null;
- }
- break;
- case XmlPullParser.END_DOCUMENT:
- break;
- }
- eventType=xmlParser.next();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- };
-