1、自定义XML文件的创建方法:
Android开发环境中,File->New->Android XML File,创建的XML位于res/xml下。
2、XML文件如下:
3、XML解析如下:
public static List parserXML(MainActivity activity, int xmlID) {
Resources res = activity.getResources();
XmlResourceParser xmlParser = res.getXml(xmlID);
List websites = new ArrayList();
try {
int eventType = xmlParser.getEventType();
// 判断是否到了文件的结尾
while (eventType != XmlResourceParser.END_DOCUMENT) {
//文件的内容的起始标签开始,注意这里的起始标签是websites.xml文件里面标签下面的第一个标签
if (eventType == XmlResourceParser.START_TAG) {
String tagname = xmlParser.getName();
if (tagname.endsWith("website")) {
Website website = new Website();
website.setCharset(xmlParser.getAttributeValue(null, "charset"));
website.setPageMark(xmlParser.getAttributeValue(null, "pageMark"));
websites.add(website);
}
}
else if (eventType == XmlResourceParser.END_TAG) {
} else if (eventType == XmlResourceParser.TEXT) {
}
eventType = xmlParser.next();
}
}catch (XmlPullParserException e) {
} catch (IOException e) {
e.printStackTrace();
}
xmlParser.close();
return websites;
}4、website定义如下:
public class Website{
private String charset;// gbk, gb2321, utf-8
private String pageMark;// page, page, pageIndex
public void setCharset(String charset){
this.charset = charset;
}
public String getCharset(){
return charset;
}
public void setPageMark(String pageMark){
this.pageMark = pageMark;
}
public String getPageMark(){
return pageMark;
}
}