Parsing XML with XmlPull_(MyMoviesWithHttpClient)
2013-01-31 13:56
295 查看
explicitly move forward through an XML document bit by bit, and skip any entity you encounter in which you’re not interested. That’s what a pull parser does.
You’re looking for a lightweight way to parse XML documents without having to keep them in memory at all times. You specifically want a parser that fetches the next token (such as an element or text) from the document explicitly rather than via callbacks (pull).
The benefit of this model is that it’s easy to skip forward until a certain element is reached, read its value, and then exit early if that’s all you need.
![](http://img.my.csdn.net/uploads/201301/31/1359612047_6874.png)
XmlPullParser and its related classes are—unlike SAX or DOM—not part of the standard JDK 5. They’re bundled with Android as a third-party library in the org.xmlpull.v1 package.
You’re looking for a lightweight way to parse XML documents without having to keep them in memory at all times. You specifically want a parser that fetches the next token (such as an element or text) from the document explicitly rather than via callbacks (pull).
The benefit of this model is that it’s easy to skip forward until a certain element is reached, read its value, and then exit early if that’s all you need.
![](http://img.my.csdn.net/uploads/201301/31/1359612047_6874.png)
XmlPullParser and its related classes are—unlike SAX or DOM—not part of the standard JDK 5. They’re bundled with Android as a third-party library in the org.xmlpull.v1 package.
//This task resolves a movie’s IMDb ID (passed as a String) to a Movie object public class GetMovieRatingTask extends AsyncTask<String,Void,Movie> { private static final String API_KEY = "624645327f33f7866355b7b728f9cd98"; private static final String API_ENDPOINT = "http://api.themoviedb.org/2.1"; private static final int PARSER_KIND_SAX = 0; private static final int PARSER_KIND_XMLPULL = 1; private static final int PARSER_KIND_JSON = 2; private int parserKind = PARSER_KIND_SAX; private Activity activity; public GetMovieRatingTask(Activity activity){ this.activity=activity; } @Override protected Movie doInBackground(String... params) { // TODO Auto-generated method stub try{ //input:IMDb ID;output:movie object String imdbId=params[0]; HttpClient httpClient=MyMovies.getHttpClient(); String format=parserKind==PARSER_KIND_JSON?"json":"Xml"; //movie address //language (/en) and the response format (/xml). //API key, which identifies our application on the web service //API key is shared among all users of the application String path= "/Movie.imdbLookup/en/" + format + "/" + API_KEY + "/"+ imdbId; HttpGet request=new HttpGet(API_ENDPOINT+path); //send service request HttpResponse response=httpClient.execute(request); InputStream data=response.getEntity().getContent(); //parse response switch(parserKind){ case PARSER_KIND_SAX: return SAXMovieParser.parseMovie(data); case PARSER_KIND_XMLPULL: return XmlPullMovieParser.parseMovie(data); case PARSER_KIND_JSON: return JsonMovieParser.parseMovie(data); default: throw new RuntimeException("unsupported parser"); } }catch(Exception e){ e.printStackTrace(); return null; } } //If parsing succeeded, we read the relevant fields from the Movie object //and show them in a pop-up dialog protected void onPostExecute(Movie movie){ if(movie==null){ Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show(); return; } Dialog dialog=new Dialog(activity); dialog.setContentView(R.layout.movie_dialog); dialog.setTitle("IMDb rating for \"" + movie.getTitle() + "\""); TextView rating=(TextView)dialog.findViewById(R.id.movie_dialog_rating); rating.setText(movie.getRating()); dialog.show(); } }
相关文章推荐
- Parsing JSON_(MyMoviesWithHttpClient)
- Parsing XML with SAX_(MyMoviesWithHttpClient)
- Consuming XML and JSON web services (MyMoviesWithHttpClient)
- HTTP with Apache HttpClient_(MyMoviesWithHttpClient)
- 利用Pull解析xml时,总抛出这个异常:Error parsing document的解决方案
- JAVA:Parsing, Modifying, and outputting XML Documents with Java
- Parsing XML Files with PowerShell
- Parsing XML Files with PowerShell
- Tutorial: Loading and parsing external XML and JSON files with Unity
- Python 101 – Intro to XML Parsing with ElementTree
- JDOM Example : Reading and Parsing XML with SAX parser in Java
- Parsing web.config with XmlDocument.SelectSingleNode
- XML Parsing with jQuery 【转】
- Android: Simplified source code for parsing and working with XML data and web services in Android
- Configuring a thread-safe HttpClient(MyMoviesWithHttpClient)
- 11.12. Parsing XML with NSXMLParser
- 利用Pull解析xml时,总抛出这个异常:Error parsing document的解决方案
- Parsing XML Files with SAX
- How-to: parsing XML with Qt
- 网络请求xml数据,并用pull方式解析