Parsing Server XML using Android pull

Source: Internet
Author: User

 

1. Three XML parsing methods in Android: pull, dom, And sax

Pull integrates this resolution in android, and its performance is similar to that of sax. I personally think it is much easier to use it than to use it for sax parsing. It uses event-driven parsing.

Dom (Document Object Model) Document Object Model: it is a method recommended by W3C organizations to parse XML. Generally, only
Only small XML files can be parsed, Because dom parsing puts the entire XML into the memory, which occupies a large amount of memory.
Easy to operate by adding, deleting, modifying, and checking tags
Sax (Simple API for XML) is not an official standard, but it is a de facto standard of the XML community. Almost all XML

The parser supports it. Sax Parsing is generally suitable for reading xml, while sax parsing reads from the top down and reads from one row;

 

Instance code:

 

Package com.android.xiong.doc umentpullxml; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import java. util. linkedHashMap; import org. xmlpull. v1.XmlPullParser; import org. xmlpull. v1.XmlPullParserException; import org. xmlpull. v1.XmlPullParserFactory; import android. app. activity; import android. app. progressDialog; import android. content. dialogInterface; import android. OS. asyncTask; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {TextView showtxt; Button btshow; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); showtxt = (TextView) findViewById (R. id. showtxt); btshow = (Button) findViewById (R. id. showxml); btshow. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {GetBaiduXmlBooks baiduxml = new custom cute (http://bcs.duapp.com/meinvlook/books.xml) ;}}// asynchronous task class GetBaiduXmlBooks extendsAsyncTask
 
  
> {ProgressDialog progress; // initialize ProgressDialog @ Overrideprotected void onPreExecute () {progress = new ProgressDialog (MainActivity. this); progress. setTitle (prompt !); Progress. setMessage (parsing the XML stored in Baidu cloud); progress. setCanceledOnTouchOutside (false); progress. setButton (ProgressDialog. BUTTON_NEUTRAL, cancel, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// cancel the task GetBaiduXmlBooks. this. cancel (true); progress. dismiss () ;}}); progress. show () ;}// perform time-consuming operations @ Overrideprotected LinkedHashMap
  
   
DoInBackground (String... params) {String xmurl = params [0]; LinkedHashMap
   
    
Map = new LinkedHashMap
    
     
(); URL url; try {url = new URL (xmurl); HttpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setConnectTimeout (10000); connection. setRequestMethod (GET); InputStream instream = connection. getInputStream (); // get the xml parser XmlPullParser parser = XmlPullParserFactory. newInstance (). newPullParser (); parser. setInput (instream, UTF-8); int type = parser. getEventType (); // start parsing the xml file while (type! = XmlPullParser. END_DOCUMENT) {if (type = XmlPullParser. START_TAG) {// obtain the start tag if (parser. getName (). equals (name) {// obtain the node value map. put (name of the book, parser. nextText ();} if (parser. getName (). equals (price) {map. put (price, parser. nextText ();} if (parser. getName (). equals (author) {map. put (author, parser. nextText ();} if (parser. getName (). equals (gender) {map. put (gender, parser. nextText ();} if (parser. getName (). equals (AGE) {map. put (age, parser. nextText () ;}} type = parser. next () ;}} catch (IOException e) {e. printStackTrace ();} catch (XmlPullParserException e) {e. printStackTrace ();} return map;} @ Overrideprotected void onPostExecute (LinkedHashMap
     
      
Result) {for (String txt: result. keySet () {showtxt. append (txt +: + result. get (txt) +);} progress. dismiss () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
     
    
   
  
 

 

 

 
      
   
 

    
     
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.