android 读取xml文件
2012-07-16 13:27:37| 分类: 默认分类 | 标签:android |字号大中小 订阅
导读:andorid读取xml文件内容方法有三种 sax dom pull,其中 pull是android自带的 ,这里主要介绍SAX,这种方法读取的效率稍微高些
项目中经常会用到一些原始的xml文件,那么我们可以定义一些xml供程序使用,xml文件放置在res\xml目录下,
通过Resources.getXML()方法访问。
项目结构如下
xml文件的内容如下:
读取XML文件的代码如下:
- <resource> <County id="1" name="浔阳区" pictureName="/mnt/sdcard/wandoujia/image/xunyang_county.jpg" pointXY="630,155,660,135,680,147,661,170,633,198,617,216,617,195,632,187,630,155" /> <County id="2" name="开发区" pictureName="/mnt/sdcard/wandoujia/image/county.png" pointXY="990,410,1060,394,1052,467,1006,481,990,410" /> <County id="3" name="庐山区" pictureName="/mnt/sdcard/wandoujia/image/city_jiujiang.jpg" pointXY="1006,484,1068,457,1089,453,1118,421,1126,490,1097,489,1060,510,1048,516,1030,579,1010,595,1006,484" /> <County id="4" name="瑞昌县" pictureName="/mnt/sdcard/wandoujia/image/city_jiujiang.jpg" pointXY="808,381,886,485,868,510,900,538,896,574,809,605,762,687,697,640,664,659,632,647,640,596,613,594,587,559,661,546,678,560,743,469,808,381" /> </resource>
package com.jun.android; import java.io.IOException; import java.util.StringTokenizer; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.content.res.Resources; import android.content.res.XmlResourceParser; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class PointXY extends Activity { private TextView myTextView = null; private Button myButton = null; private String s; private int n; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myTextView = (TextView) findViewById(R.id.tv); myButton = (Button) findViewById(R.id.button_1); myButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { int counter = 0; StringBuilder sb = new StringBuilder(""); Resources r = getResources(); XmlResourceParser xrp = r.getXml(R.xml.county); try { // 当没有达到xml的逻辑结束终点 // getEventType方法返回读取xml当前的事件 while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { if (xrp.getEventType() == XmlResourceParser.START_TAG) { String name = xrp.getName(); System.out.println(name); if (name.equals("County")) // 查找符合条件的 { counter++; // 统计第几个节点 sb.append("第" + counter + "条客户信息:" + "\n"); sb.append(xrp.getAttributeValue(0) + "\n"); sb.append(xrp.getAttributeValue(1) + "\n"); sb.append(xrp.getAttributeValue(2) + "\n"); sb.append(xrp.getAttributeValue(3) + "\n"); s = new String(xrp.getAttributeValue(3)); String[] a = s.split(","); for (int m = 0; m < s.length(); m++) { if (s.charAt(m) == ',') { n++; System.out.println(n); System.out.println(m); } } for (int i = 0; i < n+1; i++) { sb.append("坐标: " + a[i] + "\n"); } n=0; } }// 当读取到xml节点是一个元素的尾标记时 else if (xrp.getEventType() == XmlPullParser.END_TAG) { // 控制台输出xml节点结束 System.out.println(xrp.getName() + "---End!"); } // 当读取到xml节点是文本时 else if (xrp.getEventType() == XmlPullParser.TEXT) { // 输出文本 sb.append("姓名:" + xrp.getText() + "\n\n"); } xrp.next(); } myTextView.setText(sb.toString()); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } }