android中有几种解析XML文件的方法,现在呢,看一个SAX解析XML文件的实现过程
首先看看布局的实现
notes.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="android的SAX解析XML的测试" />
<ListView
android:id="@+id/noteslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
notesdetial.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingTop="15dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/notesimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:src="@drawable/icon_default_avatar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/notesheaddetial"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:textColor="#2E8B57"
android:textSize="15sp" />
<TextView
android:id="@+id/notestimedetial"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:textColor="#292421"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/notestextdetial"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#000000"
/>
</LinearLayout>
</LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingTop="15dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/notesimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:src="@drawable/icon_default_avatar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/noteshead"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:textColor="#2E8B57"
android:textSize="15sp" />
<TextView
android:id="@+id/notestime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:textColor="#292421"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/notestext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:ellipsize="end"
android:padding="15dip"
android:textColor="#000000"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:paddingBottom="10dp"
android:paddingRight="20dp" >
<TextView
android:id="@+id/pinglun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#2E8B57"
android:background="@drawable/con_btn_bg"
android:drawableLeft="@drawable/img_icon_comment"
android:text="评论" />
<TextView
android:id="@+id/zan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/pinglun"
android:textColor="#2E8B57"
android:background="@drawable/con_btn_bg"
android:drawableLeft="@drawable/img_icon_praise_clicked"
android:layout_marginRight="15dp"
android:text="赞" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
需要SAX解析的XMl文件放在raw文件下的notesax.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resource >
<noteshead> 程序员日志</noteshead>
<notestime> 1分钟前</notestime>
<notestext> 对于一个程序员来说,学习的重要性相信大家都能意识得到。如何才能有效地学习,高效地学习?探讨下这个问题,我认为很有必要。每个人学习的方法和习惯可能都不尽相同,很多人习惯了在学校的学习方式,特别是很多刚出校门的人。在他们潜意识中,学习只是应付考试的一种手段,考完就一切OK了,全部还给老师了也无所谓,反正考完之后下次不会再考这么课程了(大学尤其如此)。长期的应试教育会让很多人以考试的心态来对待学习,我也是受害者之一,翻翻我硬盘中散落的各种语言编写的"hello world",各种半成品,它们就像一张张60分、80分的试卷悄无声息地躺在某个昏暗的角落里。 </notestext>
</resource>
<resource >
<noteshead> hhe</noteshead>
<notestime> 1分钟前</notestime>
<notestext> 1111</notestext>
</resource>
<resource >
<noteshead> hhe</noteshead>
<notestime> 1分钟前</notestime>
<notestext> 222</notestext>
</resource>
<resource >
<noteshead> hhe</noteshead>
<notestime> 1分钟前</notestime>
<notestext> 3</notestext>
</resource>
</resources>
然后呢是SAX解析XML实现活动的方法notesSAX
package com.nyist.SAX;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class notesSAX extends DefaultHandler {
private List<notesGS> notesList;
private notesGS notes;
private String tagname;
public notesSAX(List<notesGS> noteslist) {
super();
this.notesList = noteslist;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String temp = new String(ch, start, length);
if (tagname.equals("noteshead")) {
notes.setNoteshead(temp);
} else if (tagname.equals("notestime")) {
notes.setNotestime(temp);
} else if (tagname.equals("notestext")) {
notes.setNotestext(temp);
}
super.characters(ch, start, length);
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (qName.equals("resource")) {
notesList.add(notes);
}
tagname="";
super.endElement(uri, localName, qName);
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("resource")) {
notes=new notesGS();
}
tagname=localName;
super.startElement(uri, localName, qName, attributes);
}
}
SAX解析XML文件的Get和Set的方法notesGS
package com.nyist.SAX;
public class notesGS {
private String noteshead;
private String notestime;
private String notestext;
public String getNoteshead() {
return noteshead;
}
public void setNoteshead(String noteshead) {
this.noteshead = noteshead;
}
public String getNotestime() {
return notestime;
}
public void setNotestime(String notestime) {
this.notestime = notestime;
}
public String getNotestext() {
return notestext;
}
public void setNotestext(String notestext) {
this.notestext = notestext;
}
public notesGS() {
super();
}
public void notesdetial(String noteshead, String notestime, String notestext) {
this.noteshead = noteshead;
this.notestext = notestext;
this.notestime = notestime;
}
}
SaxXmlDemoActivity
package com.nyist.activity;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.nyist.SAX.notesGS;
import com.nyist.SAX.notesSAX;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class SaxXmlDemoActivity extends Activity {
private ListView noteslistView;
private MyAdapter adapter=null;
List<notesGS> list=new ArrayList<notesGS>();
private int images[]={R.drawable.icon_default_avatar};
// private int images[]={R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,
// R.drawable.b5,R.drawable.b6,R.drawable.b7,R.drawable.b8,R.drawable.b9,
// R.drawable.b10,R.drawable.b11,R.drawable.b12,R.drawable.b13,R.drawable.b14};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
noteslistView=(ListView)findViewById(R.id.noteslist);
SAXParserFactory notesfactory = SAXParserFactory.newInstance();
try {
XMLReader reader = notesfactory.newSAXParser().getXMLReader();
notesSAX notesSAX=new notesSAX(list);
reader.setContentHandler(notesSAX);
InputStream ip = getResources().openRawResource(R.raw.notesax);
reader.parse(new InputSource(ip));
} catch (Exception e) {
e.printStackTrace();
}
adapter = new MyAdapter(this,list);
noteslistView.setAdapter(adapter);
// Toast.makeText(SaxXmlDemoActivity.this, "亲、你点击了我", Toast.LENGTH_SHORT).show();
noteslistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(SaxXmlDemoActivity.this,NotesDetialText.class);
Toast.makeText(SaxXmlDemoActivity.this, "亲、你点", Toast.LENGTH_SHORT).show();
intent.putExtra("image", arg2);
intent.putExtra("noteshead", list.get(arg2).getNoteshead());
intent.putExtra("notestime", list.get(arg2).getNotestime());
intent.putExtra("notestext", list.get(arg2).getNotestext());
startActivity(intent);
}
});
//Toast.makeText(SaxXmlDemoActivity.this, "亲.......", Toast.LENGTH_SHORT).show();
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater myinInflater;
private List<notesGS> list;
public List<notesGS> getList(){
return list;
}
public MyAdapter(Context context, List<notesGS> list) {
this.myinInflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//
holder = new ViewHolder();
convertView = myinInflater.inflate(R.layout.listview_item, null);
holder.noteshead = (TextView) convertView
.findViewById(R.id.noteshead);
holder.notestime = (TextView) convertView
.findViewById(R.id.notestime);
holder.notestext = (TextView) convertView
.findViewById(R.id.notestext);
holder.image=(ImageView)convertView.findViewById(R.id.notesimg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 设置各个Item中的组件
holder.noteshead.setText(list.get(position).getNoteshead());
holder.notestime.setText(list.get(position).getNotestime());
holder.notestext.setText(list.get(position).getNotestext());
holder.image.setImageResource(images[0]);
return convertView;
}
}
public final class ViewHolder {
public ImageView image;
public TextView noteshead;
public TextView notestime;
public TextView notestext;
}
}
NotesDetialText
package com.nyist.activity;
import com.nyist.activity.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class NotesDetialText extends Activity {
private int images[]={R.drawable.icon_default_avatar};
private TextView noteshead,notestime,notestext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notesdetial);
Intent intent=null;
intent = getIntent();
noteshead=(TextView)findViewById(R.id.notesheaddetial);
notestime=(TextView)findViewById(R.id.notestimedetial);
notestext=(TextView)findViewById(R.id.notestextdetial);
noteshead.setText(intent.getStringExtra("noteshead"));
notestime.setText(intent.getStringExtra("notestime"));
notestext.setText(intent.getStringExtra("notestext"));
}
}
ps:运行结果如下: