首页 > 技术文章 > Android 解析XML—pull解析方式

boket 2017-05-03 14:37 原文

在Android中,常见的XML解析器分别为SAX解析器、DOM解析器和PULL解析器,其中PULL解析器小巧轻便,解析速度快,简单易用,非常适合在Android移动设备中使用,Android系统内部在解析各种XML时也是用PULL解析器,今天我来介绍一下PULL解析器

首先是在Tomcat服务器上建了一个xml的文档具体如下:

通过访问网络与服务器的交互。

下面是具体的代码:

  1 package com.hb.xml;
  2 
  3 import java.io.InputStream;
  4 import java.net.HttpURLConnection;
  5 import java.net.URL;
  6 
  7 import org.xmlpull.v1.XmlPullParser;
  8 
  9 import android.app.Activity;
 10 import android.os.Bundle;
 11 import android.os.Handler;
 12 import android.os.Message;
 13 import android.util.Xml;
 14 import android.view.View;
 15 import android.widget.Button;
 16 import android.widget.EditText;
 17 import android.widget.TextView;
 18 import android.widget.Toast;
 19 
 20 public class MainActivity extends Activity {
 21     protected static final int NUMBER = 0;
 22     protected static final int LOCATION = 1;
 23     protected static final int PHONEJX = 2;
 24     protected static final int NO = 3;
 25     private Button bt_start;
 26     private TextView tv_desc;
 27     private TextView tv_number;
 28     private TextView tv_for;
 29     private String path;
 30     private XmlPullParser xml;
 31     private Handler handler=new Handler(){
 32         public void handleMessage(android.os.Message msg) {
 33             switch (msg.what) {
 34             case NUMBER:
 35                 tv_number.setText((String)msg.obj);
 36                 Toast.makeText(MainActivity.this, "测试成功", 0).show();
 37                 break;
 38             case LOCATION:
 39                 tv_for.setText((String)msg.obj);
 40                 Toast.makeText(MainActivity.this, "测试成功", 0).show();
 41                 break;
 42             case PHONEJX:
 43                 tv_desc.setText((String)msg.obj);
 44                 Toast.makeText(MainActivity.this, "测试成功", 0).show();
 45                 break;
 46             case NO:
 47                 Toast.makeText(MainActivity.this, "测试失败", 0).show();
 48                 break;
 49             }
 50         };
 51     };
 52 
 53     @Override
 54     protected void onCreate(Bundle savedInstanceState) {
 55         super.onCreate(savedInstanceState);
 56         setContentView(R.layout.activity_main);
 57         initView();
 58     }
 59 
 60     private void initView() {
 61         bt_start=(Button) findViewById(R.id.bt_start);
 62         tv_desc=(TextView) findViewById(R.id.tv_desc);
 63         tv_number=(TextView) findViewById(R.id.tv_number);
 64         tv_for=(TextView) findViewById(R.id.tv_for);
 65     }
 66     public void testtesting(View v){
 67         new Thread(){
 68             
 69             public void run() {
 70                 try {
 71                     URL url = new URL("http://192.168.1.104:8080/test.xml");
 72                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 73                     conn.setConnectTimeout(3000);
 74                     conn.setRequestMethod("GET");
 75                     int code = conn.getResponseCode();
 76                     if(code==200){
 77                         
 78                         InputStream is = conn.getInputStream();
 79                         xml = Xml.newPullParser();
 80                         xml.setInput(is, "gbk");
 81                         int type = xml.getEventType();
 82                         while (type!=XmlPullParser.END_DOCUMENT) {
 83                             if(type == XmlPullParser.START_TAG){
 84                                 if("phonenum".equals(xml.getName())){
 85                                     String phonenum=xml.nextText();
 86                                     Message msg= new Message();
 87                                     msg.what=NUMBER;
 88                                     msg.obj=phonenum;
 89                                     handler.sendMessage(msg);
 90                                 }else if("location".equals(xml.getName())){
 91                                     String location=xml.nextText();
 92                                     Message msg= new Message();
 93                                     msg.what=LOCATION;
 94                                     msg.obj=location;
 95                                     handler.sendMessage(msg);
 96                                 }else if("phoneJx".equals(xml.getName())){
 97                                     String desc=xml.nextText();
 98                                     Message msg= new Message();
 99                                     msg.what=PHONEJX;
100                                     msg.obj=desc;
101                                     handler.sendMessage(msg);
102                                 }
103                             }else{
104                                 //测试失败
105                             }
106                             type=xml.next();
107                         }
108                     }
109                 } catch (Exception e) {
110                     e.printStackTrace();
111                     Message msg= new Message();
112                     msg.what=NO;
113                     handler.sendMessage(msg);
114                 }
115             };
116         }.start();
117     }
118 }

最后别忘记添加访问网络的权限:

<uses-permission android:name="android.permission.INTERNET"/>

 

简单的布局:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context="${relativePackage}.${activityClass}" >
 7 
 8 
 9     <Button
10         android:id="@+id/bt_start"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_gravity="center_horizontal"
14         android:onClick="testtesting"
15         android:text="测试" />
16 
17    
18     <TextView
19         android:id="@+id/tv_number"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="内容"
23         android:textColor="#F4A460" />
24 
25     <TextView
26         android:id="@+id/tv_for"
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:text="信息"
30         android:textColor="#99FF33" />
31      <TextView
32         android:id="@+id/tv_desc"
33         android:layout_width="match_parent"
34         android:layout_height="wrap_content"
35         android:text="描述"
36         android:textColor="#EE30A7" />
37     
38 
39 </LinearLayout>
View Code

源码下载地址:http://pan.baidu.com/s/1geBFWLd

 

推荐阅读