Xml解析

来源:互联网 发布:c语言程序编程 编辑:程序博客网 时间:2024/06/16 08:23
public void getxml() {    try {        HttpClient client = new DefaultHttpClient();        HttpGet get = new HttpGet(url);        HttpResponse response = client.execute(get);        int code = response.getStatusLine().getStatusCode();        if (code == 200) {            InputStream inputStream = response.getEntity().getContent();            XmlPullParserFactory factory = XmlPullParserFactory                    .newInstance();            XmlPullParser parser = factory.newPullParser();            parser.setInput(inputStream, "utf-8");            int type = parser.getEventType();            while (type != XmlPullParser.END_DOCUMENT) {                String str_name = parser.getName();                if (type == XmlPullParser.START_TAG) {                    if ("firstImg".equals(str_name)) {                        wanml = new Wanml();                        wanml.setFirstImg(parser.nextText());                    } else if ("id".equals(str_name)) {                        wanml.setId(parser.nextText());                    } else if ("source".equals(str_name)) {                        wanml.setSource(parser.nextText());                    } else if ("title".equals(str_name)) {                        wanml.setTitle(parser.nextText());                    } else if ("url".equals(str_name)) {                        wanml.setUrl(parser.nextText());                    }                } else if (type == XmlPullParser.END_TAG) {                    list.add(wanml);                }                type = parser.next();            }            System.out.println(list.toString());        }    } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}
0 0
原创粉丝点击