xmlpull解析l类:Hdfans_new
import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; //import android.annotation.SuppressLint; import android.util.Log; import android.util.SparseArray; public class SearchInfoXmlPullParser extends XmlPullParserBase2 { private static final String TAG="SearchInfoXmlPullParser"; // @SuppressLint("UseSparseArrays") public static SparseArray<ArrayList<Media_search>> getResourceList(String Url,String[] str){ SparseArray<ArrayList<Media_search>> media_searchs = new SparseArray<ArrayList<Media_search>>(); ArrayList<Media_search> ms01= null; ArrayList<Media_search> ms02= new ArrayList<Media_search>(); ArrayList<Media_search> ms03= new ArrayList<Media_search>(); ArrayList<Media_search> ms04= new ArrayList<Media_search>(); ArrayList<Media_search> ms05= new ArrayList<Media_search>(); ArrayList<Media_search> ms06= new ArrayList<Media_search>(); Media_search media_s=null; Media media=null; XmlPullParser parser=null; parser=getUriXmlPullParser(Url); if(parser!=null){ try { int eventType=parser.getEventType(); while(eventType!=XmlPullParser.END_DOCUMENT){ switch(eventType){ case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: String name= parser.getName(); if(name.equalsIgnoreCase("ROWS")){ ms01= new ArrayList<Media_search>(); }else if(name.equalsIgnoreCase("foobar")){ media= new Media(); media_s= new Media_search(); }else if(name.equalsIgnoreCase("id")){ String s=parser.nextText(); if(s.equals("0")){ return null; //û����������� }else{ media.setId(Long.parseLong(s)); } }else if(name.equalsIgnoreCase("name")){ media.setMed_name(parser.nextText()); Log.i(TAG, media.getMed_name()); }else if(name.equalsIgnoreCase("img")){ media.setMed_img_url(parser.nextText()); Log.i(TAG, media.getMed_img_url()); }else if(name.equalsIgnoreCase("link")){ media.setLink_url(parser.nextText()); Log.i(TAG, media.getLink_url()); }else if(name.equalsIgnoreCase("type")){ media_s.setType(parser.nextText()); Log.i(TAG, media_s.getType()); } break; case XmlPullParser.END_TAG: if(parser.getName().equalsIgnoreCase("foobar")){ media_s.setMedia(media); ms01.add(media_s); String type=media_s.getType().trim(); if(type.equals(str[0])){ ms02.add(media_s); }else if(type.equals(str[1])){ ms03.add(media_s); }else if(media_s.getType().equals(str[2])){ ms04.add(media_s); }else if(type.equals(str[3])){ ms05.add(media_s); }else if(type.equals(str[4])){ ms06.add(media_s); } } break; } eventType = parser.next(); } } catch (MalformedURLException e) { Log.i(TAG, e.toString()); return null; } catch (IOException e) { Log.i(TAG, e.toString()); return null; } catch (XmlPullParserException e) { Log.i(TAG, e.toString()); return null; } media_searchs.put(1, ms01); media_searchs.put(2, ms02); media_searchs.put(3, ms03); media_searchs.put(4, ms04); media_searchs.put(5, ms05); media_searchs.put(6, ms06); return media_searchs; } return null; } }
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.util.Log; public class XmlPullParserBase2 { private static final String TAG="XmlPullParserBase2"; public static XmlPullParser getUriXmlPullParser(String url) { XmlPullParser parser = null; try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); parser = factory.newPullParser(); } catch (XmlPullParserException e) { Log.e(TAG, "Unable to create XmlPullParser", e); return null; } InputStream inputStream = null; HttpURLConnection conn=null; try{ URL Url = new URL(url.toString()); conn = (HttpURLConnection) Url.openConnection(); conn.setDoInput(true); conn.setConnectTimeout(4000); conn.connect(); inputStream = conn.getInputStream(); // String str=InputStreamTOString(inputStream,"utf-8"); // Log.i(TAG, str); try { if(inputStream!=null) parser.setInput(inputStream, "UTF-8"); } catch (XmlPullParserException e) { Log.w(TAG, "Error while reading XML file from " + url, e); return null; } }catch(Exception e){ Log.i(TAG, e.toString()+"@@@@@@@@@@@@@@@@@@@"); }finally{ // conn.disconnect(); } return parser; } }