主要的Acivityjava
package com.xml.xml; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * 主Activity * @author 吕檀溪 * */ public class MainActivity extends Activity { private Button but; public static List<Map<String, String>> list=new ArrayList<Map<String,String>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); but=(Button) findViewById(R.id.but); but.setOnClickListener(new ButtonClick()); } class ButtonClick implements OnClickListener{ @Override public void onClick(View view) { //获得产生SAX的生产工厂 SAXParserFactory factory=SAXParserFactory.newInstance(); try { //获得获取XML的对象 XMLReader reader=factory.newSAXParser().getXMLReader(); //绑定读书数据的类 reader.setContentHandler(new MySaxRead()); //获取须要读取的.xml文件 reader.parse(new InputSource(MainActivity.class.getClassLoader().getResourceAsStream("user.xml"))); //绑定数据 Intent intent=new Intent(); intent.putExtra("list", (Serializable)list); intent.setClass(MainActivity.this, ArrayActivity.class); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } } }
解析xml的Acitityandroid
package com.xml.xml; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * 读取xml文件的类,继承了DefaultHandler * 这里的五个方法都是从DefaultHandler重写的 * @author 吕檀溪 * */ public class MySaxRead extends DefaultHandler{ private String tagName; private List<Map<String, String>> list=new ArrayList<Map<String,String>>(); private String name; //结束Document的方法,并给MainActivity连的list赋值 @Override public void endDocument() throws SAXException { MainActivity.list=this.list; } //结束标签的读取,并给结束标签添加一个标记 @Override public void endElement(String uri, String localName, String qName) throws SAXException { tagName=File.separator+localName; } //开始读取内容 @Override public void startDocument() throws SAXException { } //开始读取标签 @Override public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException { //获取没有前缀的标签名 tagName=localName; //判断是不是咱们须要的标签 if("worker".equals(tagName)){ //循环获得标签的属性 for (int i = 0; i < attr.getLength(); i++) { System.out.println(attr.getLocalName(i)+"=="+attr.getValue(i)); } } } //获得便签的值 @Override public void characters(char[] ch, int start, int length) throws SAXException { if("name".equals(tagName)){ name=new String(ch, start, length); }else if("age".equals(tagName)){ //这个步骤很关键,由于这里没作好就会出现空值的现象;这里是先把name的值存在全局变量中 //而后在根据age的非空值一块儿存进map集合 Map<String, String> map=new HashMap<String, String>(); String age=new String(ch, start, length); map.put("name", name); map.put("age", age); list.add(map); } } }
列表显示的Activityapp
package com.xml.xml; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.widget.SimpleAdapter; /** * 作一个列表显示 * @author 吕檀溪 * */ public class ArrayActivity extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.array); //获得传过来获得Intent Intent intent=getIntent(); //获得在Intent绑定数数据 List<Map<String, String>> list=(List<Map<String, String>>) intent.getSerializableExtra("list"); //给SimpleAdapter绑定须要显示的数据,以及制定显示的规范 SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.user, new String[]{"name","age"}, new int[]{R.id.name,R.id.age}); setListAdapter(adapter); } }
主要的布局文件main.xmlide
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/but" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="解析XML" /> </RelativeLayout>
配置须要列表显示的文件布局
一、申明列表文件array.xmlthis
<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" > <ListView android:id="@+id/android:list" android:layout_height="wrap_content" android:layout_width="fill_parent" android:drawSelectorOnTop="false" android:scrollbars="vertical" ></ListView> </LinearLayout>
二、数据显示文件user.xmlcode
<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="horizontal" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20pt" /> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20pt" /> </LinearLayout>
注册Activity的文件AndroidManifest.xmlxml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xml.xml" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.xml.xml.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.xml.xml.ArrayActivity"></activity> </application> </manifest>
最后是须要解析的xml文件user.xml,放在src文件目录下对象
<?xml version="1.0" encoding="utf-8"?> <workers> <worker id="001" nameMy="001x"> <name>迈克儿.乔丹</name> <age>17</age> </worker> <worker id="002" nameMy="002x"> <name>周杰伦</name> <age>18</age> </worker> </workers>