本人在百度云存储的XMLhttp://bcs.duapp.com/meinvlook/books.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("http://bcs.duapp.com/meinvlook/books.xml");
}
});
}
// 异步任务
class GetBaiduXmlBooks extends
AsyncTask<String, Integer, LinkedHashMap<String, String>> {
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<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();
// 获取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<String, String> result) {
for (String txt : result.keySet()) {
showtxt.append(txt+":"+result.get(txt)+"\n");
}
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;
}
}
<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>
<!-- 获取网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>