代码目录结构
HobInfo实体类代码
package cn.itcast.person;
import android.R.integer;
public class HobInfo {
private int id;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
private String message;
}
解析器实现代码
package cn.itcast.person;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Parser;
import org.xmlpull.v1.XmlPullParser;
import android.R.integer;
import android.util.Xml;
public class HobSeriver {
public static List<HobInfo>serivers( InputStream is)throws Exception{
XmlPullParser parser=Xml.newPullParser();
parser.setInput(is, "utf-8");
List<HobInfo> hobInfos=null;
HobInfo hobInfo=null;
int type=parser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("infos".equals(parser.getName())){
hobInfos=new ArrayList<HobInfo>();
}else if("person".equals(parser.getName())){
hobInfo=new HobInfo();
String ids=parser.getAttributeValue(0);
hobInfo.setId(Integer.parseInt(ids));
}else if("city".equals(parser.getName())){
String cString=parser.nextText();
hobInfo.setCity(cString);
}
else if("message".equals(parser.getName())){
String mString=parser.nextText();
hobInfo.setMessage(mString);
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
hobInfos.add(hobInfo);
hobInfo=null;
}
break;
}
type=parser.next();
}
return hobInfos;
}
}
package cn.itcast.person;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Map<String, String> map;
private List<Map<String, String>> list;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
textView=(TextView)findViewById(R.id.textview);
try{
List<HobInfo> infos=HobSeriver.serivers(MainActivity.this.getClassLoader().getResourceAsStream("person.xml"));
list=new ArrayList<Map<String,String>>();
for(HobInfo a:infos){
map=new HashMap<String, String>();
map.put("city", a.getCity());
map.put("message", a.getMessage());
list.add(map);
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "解析失败", Toast.LENGTH_SHORT);
}
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getMaps(0);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getMaps(1);
}
});
}
private void getMaps(Integer i){
Map<String, String> bjmapMap=list.get(i);
String messageString=bjmapMap.get("message");
textView.setText(messageString);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
然后给上xml布局文件
<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"
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="cn.itcast.person.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="没有选择按钮" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天津" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
最后清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.person"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.itcast.person.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>
</application>
</manifest>
值得注意的是package的命名,建议类似cn.musa.dad的结构,而且如果更改包名,请注意在运行之前在更改清单文件之中的相应包名