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