首页 > 代码库 > android 读取xml
android 读取xml
在有些应用中,有一点小数据,直接存储在XML就是,实现较为简单,
1、xml文件放入asset文件夹,结构如:
<?xml version="1.0" encoding="UTF-8" ?>
<messages><message id="1"><title>3月毕业证数码像采集</title><date1>3</date1><content>3月12日左右全年一次毕业证数码像采集,由新华社负责</content><url>23</url></message><message id="2"><title>3、9月课程注册</title><date1>3</date1><content>3月15日、9月15日本学期所修课程注册、课本征订,包括课程修完学员论文注册</content><url>23</url>
</message>
</messages>
2 、构建message类,解析xml类PullParseService
3、在activity中调用。
具体代码如下:
1.解析xml类
package com.edu.notice;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class PullParseService {
public static ArrayList <Message>getMessages(InputStream inputStream) throws Exception{
ArrayList<Message> messages =null;
Message message =null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "UTF-8");
int event = parser.getEventType();//
while(event!=XmlPullParser.END_DOCUMENT){
switch(event){
case XmlPullParser.START_DOCUMENT://
messages = new ArrayList<Message>();//
break;
case XmlPullParser.START_TAG://
if("message".equals(parser.getName())){//
message = new Message();
message.setId(Integer.parseInt(parser.getAttributeValue(0)));//
}
if(message!=null){
if("title".equals(parser.getName())){//
message.setTitle(parser.nextText());
}else if("date1".equals(parser.getName())){//
message.setData1(Integer.parseInt(parser.nextText()));
}else if("content".equals(parser.getName())){
message.setContent(parser.nextText());
}else if("url".equals(parser.getName())){
message.setUrl(parser.nextText());
}
}
break;
case XmlPullParser.END_TAG://
if("message".equals(parser.getName())){//
messages.add(message);//
message = null;
}
break;
}
event = parser.next();//
}//end while
return messages;
};
}
2、xml每个对象信息类
package com.edu.notice;
public class Message {
private int id;
private String title;
private int data1;
private String content;
private String url;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the data1
*/
public int getData1() {
return data1;
}
/**
* @param data1 the data1 to set
*/
public void setData1(int data1) {
this.data1 = data1;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
}
3、读取xml数据应用到应用中
AssetManager asset = getAssets();
try {
InputStream input=asset.open("message.xml");
List<Message> list = null;
try {
list = PullParseService.getMessages(input);
String tag="xmllllll";
int id =(int) (Math.random()*12);
Log.i(tag, list.get(id).getContent());
//将内容发布广播
setReminder(true, list.get(id).getContent());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}