Pull parse XML files in much the same way that sax parses XML files, all based on event-driven. Therefore, the following steps are required to parse an XML file using Pull:
1) Get Xmlpullparser object through Xmlpullparserfactory.
2 The input stream is set through the Xmlpullparser object.
3 through Parser.next (), continuous parsing of XML files until the end of the file.
Several of the following methods are often required: xmlpullparserfacotry.newinstance () Facotry.newpullparser () Parser.setinput () Parser.next ().
The following steps are described in a few examples above:
//1. The first step is to create a resolution factory xmlpullparserfactory factory = Xmlpullparserfactory.newinstance ();
Set support for Namespace Factory.setnamespaceaware (true);
2. Generate parser Object Xmlpullparser parser = Factory.newpullparser (); 3. Set input Parser.setinput (new StringReader ("<?xml version=\" 1.0\ "?><poem><title>" Quiet Night thinking </title
><author> Li Bai </author><content> bed before Ming moonlight, suspected on the ground frost, the first to forget the Moon, head home </content></poem> ")");
Gets the input type int eventtype = Parser.geteventtype (); while (EventType!= xmlpullparser.end_document) {if (EventType = = xmlpullparser.start_document) {log.d ("tag", "---
-"+parser.getname ()");
} if (EventType = = Xmlpullparser.start_tag) {log.d ("TAG", "----" +parser.getname ());
} if (EventType = = Xmlpullparser.text) {log.d ("tag", "----" +parser.gettext ());
} if (EventType = = Xmlpullparser.end_tag) {log.d ("TAG", "----" +parser.getname ());
//constantly update eventtype = Parser.next (); }
Printed objects:
Pull integrates this parsing on Android, which is similar in performance to sax, and is used by individuals to be much easier to parse than sax parsing, with event-driven parsing.
The DOM (Document Object model) Documentation object Model: a way to parse XML that is recommended by the World Wide organization; You can only parse relatively small XML files; Because Dom parsing is putting the entire XML into memory, it takes up a lot of memory, but the document's
Additions and deletions to check the standard glue easy to operate.
Sax (Simple API for XML) is not an official standard, but it is the de facto standard of the XML community, and almost all XML parsers support it. Sax parsing is generally appropriate for XML reading, and Sax parsing is read from top down and one line to read.
To see a complete example:
Package com.android.xiong.documentpullxml;
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;
@Override protected 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 () {@Override public void OnClick (View v) {G
Etbaiduxmlbooks baiduxml=new getbaiduxmlbooks ();
Baiduxml.execute ("Http://bcs.duapp.com/meinvlook/books.xml");
}
}); }//Asynchronous task Class Getbaiduxmlbooks extends Asynctask<string, Integer, linkedhashmap<string, string>& Gt
{ProgressDialog progress; Initialize ProgressDialog @Override protected void OnPreExecute () {progress = new ProgressDialog (mainactivity
. this); Progress.settitle ("Hint!")
");
Progress.setmessage ("Parsing the XML stored in Baidu Cloud");
Progress.setcanceledontouchoutside (FALSE);
Progress.setbutton (Progressdialog.button_neutral, "Cancel", new Dialoginterface.onclicklistener () { @Override public void OnClick (Dialoginterface dialog, int which) {//Cancel task Getb
AiduXmlBooks.this.cancel (TRUE); ProgRess.dismiss ();
}
});
Progress.show ();
//Time-consuming operation @Override protected linkedhashmap<string, string> doinbackground (String ... params) {
String xmurl = params[0];
linkedhashmap<string, string> map = new linkedhashmap<string, string> ();
URL url;
try {url = new URL (xmurl);
HttpURLConnection connection = (httpurlconnection) URL. OpenConnection ();
Connection.setconnecttimeout (10000);
Connection.setrequestmethod ("get");
InputStream instream = Connection.getinputstream ();
Gets the XML parser xmlpullparser parser = Xmlpullparserfactory.newinstance (). Newpullparser ();
Parser.setinput (instream, "UTF-8");
int Type=parser.geteventtype ();
Starts parsing the XML file while (type!= xmlpullparser.end_document) {if (type = = Xmlpullparser.start_tag) { Gets the start tag if (parSer.getname (). Equals ("title")) {//Get the value of the node Map.put ("title", 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; @Override protected void OnPostExecute (linkedhashmap<string, string> result) {for (String TX
T:result.keyset ()) {Showtxt.append (txt+ ":" +result.get (TXT) + "\ n"); } Progress.dismiss (); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds items to th
E Action Bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_parent" "Android:orientation=" vertical "tools:context=". Mainactivity "> <button android:id=" @+id/showxml "android:layout_width=" Match_parent "Android
: layout_height= "wrap_content" android:text= "@string/btshowxml"/> <textview android:id= "@+id/showtxt" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content"/> </LinearLayout> <!- -Get Network permissions--> <uses-permission android:name= "Android.permission.INTERNET"/>