xml解析到listview里

这个博客展示了如何从远程URL获取XML数据,使用XMLPullParser解析XML内容,并将解析后的数据填充到ListView中。首先,通过HttpClient发起HTTP请求获取XML字符串,然后利用XmlPullParser进行解析,最后将解析出的Bean对象添加到列表中展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Main`package com.example.xml;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
List list=new ArrayList();
private ListView listView;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
String xml = (String) msg.obj;

        getxml(xml);
        listView.setAdapter(new MyAdapter(list, MainActivity.this));
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                MyBean myBean = list.get(position);
                Intent intent = new Intent(MainActivity.this,
                        SecondActivity.class).putExtra("url", myBean.getLink());
                startActivity(intent);


            }
        });

    };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.lv);

    new Thread() {
        public void run() {
            String string = Data.Data();

            getxml(string);
            Message message = Message.obtain();
            message.obj = string;
            handler.sendMessage(message);
        }
    }.start();
}

private void getxml(String is) {
    try {
        XmlPullParser xpp = Xml.newPullParser();
        xpp.setInput(new StringReader(is));
        int type = xpp.getEventType();
        MyBean mb = null;
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_TAG:
                if (xpp.getName().equals("item")) {
                    mb = new MyBean();
                } else if (xpp.getName().equals("title")) {
                    mb.setTitle(xpp.nextText());

                }else if(xpp.getName().equals("link")){
                    mb.setLink(xpp.nextText());

                }else if (xpp.getName().equals("imgs")) {
                    mb.setImgs(xpp.nextText());

                }

                break;
            case XmlPullParser.END_TAG:
                if (xpp.getName().equals("item")) {
                    list.add(mb);
                }
                break;
            }
            type = xpp.next();

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

Data
package com.example.xml;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Data {
public static String Data() {
String path = “http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20“;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);

            return string;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

}
`
MyAdapter

package com.example.xml;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Data {
    public static String Data() {
        String path = "http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20";
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(path);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String string = EntityUtils.toString(entity);

                return string;
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

}

Bean`package com.example.xml;

public class MyBean {
private String title;
private String link;
private String imgs;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getImgs() {
    return imgs;
}

public void setImgs(String imgs) {
    this.imgs = imgs;
}

@Override
public String toString() {
    return "MyBean [title=" + title + ", link=" + link + ", imgs=" + imgs
            + "]";
}

}
`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值