Android之pull解析服務端的XML

 

1、Android中XML三種解析方式三種方式:pull、dom和sax

pull在android 集成瞭這種解析,性能和sax差不多,個人認為用來來比sax解析容易多瞭;采用事件驅動進行解析。

dom(Document Object Model)文檔對象模型:是W3C組織推薦的解析XML的一種方式;一般隻能
隻能解析比較小的XML文件;因為dom解析是把整個XML放入內存,占用內存比較大,但對文檔的
增刪改查標膠容易操作
sax(Simple API for XML)不是官方的標準,但它是XML社區事實上的標準,幾乎所有的XML

解析器都支持它。sax解析一般適合xml的讀取,sax解析是從上往下讀取,一行一行來讀;

 

實例代碼:

 

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) {
				GetBaiduXmlBooks  baiduxml=new GetBaiduXmlBooks();
				baiduxml.execute(https://bcs.duapp.com/meinvlook/books.xml);
			}
		});
	}

	// 異步任務
	class GetBaiduXmlBooks extends
			AsyncTask> {

		ProgressDialog progress;

		// 初始化ProgressDialog
		@Override
		protected void onPreExecute() {
			progress = new ProgressDialog(MainActivity.this);
			progress.setTitle(提示!);
			progress.setMessage(正在解析百度雲存儲的XML);
			progress.setCanceledOnTouchOutside(false);
			progress.setButton(ProgressDialog.BUTTON_NEUTRAL, 取消,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// 取消任務
							GetBaiduXmlBooks.this.cancel(true);
							progress.dismiss();
						}
					});
			progress.show();
		}

		// 進行耗時操作
		@Override
		protected 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();
				// 獲取xml解析器
				XmlPullParser parser = XmlPullParserFactory.newInstance()
						.newPullParser();
				parser.setInput(instream, UTF-8);
				int type=parser.getEventType();
				//開始解析xml文件
				while (type!= XmlPullParser.END_DOCUMENT) {
					if (type == XmlPullParser.START_TAG) {
						// 獲取開始標簽
						if (parser.getName().equals(書名)) {
							//獲取節點的值
							map.put(書名, parser.nextText());
						}
						if (parser.getName().equals(價格)) {
							map.put(價格, parser.nextText());
						}
						if (parser.getName().equals(作者)) {
							map.put(作者, parser.nextText());
						}
						if (parser.getName().equals(性別)) {
							map.put(性別, parser.nextText());
						}
						if (parser.getName().equals(年齡)) {
							map.put(年齡,parser.nextText());
						}
					}
					type=parser.next();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}
			return map;
		}

		@Override
		protected void onPostExecute(LinkedHashMap result) {

			for (String txt : result.keySet()) {
				showtxt.append(txt+:+result.get(txt)+
);
			}
			progress.dismiss();
			
		}

	}

	@Override
	public 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;
	}

}

 

 

    
    

發佈留言