xml解析
2016-05-23 00:14
281 查看
解析Res下xml文件中的文件msg.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<msgs>
<msg>
<title>Tatata</title>
<content>CaCACA</content>
</msg>
<msg>
<title>TCTCTC</title>
<content>你们好</content>
</msg>
</msgs>
第二步:MainActivity.java中的代码如下:
public class MainActivity extends ListActivity {
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
List<Map<String,String>> data=parseXml(R.xml.msg);
adapter = new SimpleAdapter(this,
data,
android.R.layout.simple_list_item_2,
new String[]{"title","content"},
new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
}
private List<Map<String, String>> parseXml(int msg){
XmlResourceParser xpp = getResources().getXml(msg);
List<Map<String,String>> list=null;
Map<String,String> map=null;
try {
int event = xpp.getEventType();
do {
if(event==xpp.START_DOCUMENT){
list=new ArrayList<Map<String,String>>();
}else if(event==xpp.START_TAG){
map=processStartTag(xpp,map);
}else if(event==xpp.END_TAG&&"msg".equals(xpp.getName())){
map=processEndTag(list,map);
}
event=xpp.next();
} while (event!=xpp.END_DOCUMENT);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private Map<String, String> processStartTag(XmlResourceParser xpp,
Map<String, String> map) throws Exception, Exception {
if("msg".equals(xpp.getName())){
map=new HashMap<String,String>();
}else if("title".equals(xpp.getName())){
map.put(xpp.getName(),xpp.nextText());
}else if("content".equals(xpp.getName())){
map.put(xpp.getName(),xpp.nextText());
}
return map;
}
private Map<String, String> processEndTag(List<Map<String, String>> list,
Map<String, String> map) {
list.add(map);
map=null;
return map;
}
}
<?xml version="1.0" encoding="utf-8"?>
<msgs>
<msg>
<title>Tatata</title>
<content>CaCACA</content>
</msg>
<msg>
<title>TCTCTC</title>
<content>你们好</content>
</msg>
</msgs>
第二步:MainActivity.java中的代码如下:
public class MainActivity extends ListActivity {
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
List<Map<String,String>> data=parseXml(R.xml.msg);
adapter = new SimpleAdapter(this,
data,
android.R.layout.simple_list_item_2,
new String[]{"title","content"},
new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
}
private List<Map<String, String>> parseXml(int msg){
XmlResourceParser xpp = getResources().getXml(msg);
List<Map<String,String>> list=null;
Map<String,String> map=null;
try {
int event = xpp.getEventType();
do {
if(event==xpp.START_DOCUMENT){
list=new ArrayList<Map<String,String>>();
}else if(event==xpp.START_TAG){
map=processStartTag(xpp,map);
}else if(event==xpp.END_TAG&&"msg".equals(xpp.getName())){
map=processEndTag(list,map);
}
event=xpp.next();
} while (event!=xpp.END_DOCUMENT);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private Map<String, String> processStartTag(XmlResourceParser xpp,
Map<String, String> map) throws Exception, Exception {
if("msg".equals(xpp.getName())){
map=new HashMap<String,String>();
}else if("title".equals(xpp.getName())){
map.put(xpp.getName(),xpp.nextText());
}else if("content".equals(xpp.getName())){
map.put(xpp.getName(),xpp.nextText());
}
return map;
}
private Map<String, String> processEndTag(List<Map<String, String>> list,
Map<String, String> map) {
list.add(map);
map=null;
return map;
}
}
相关文章推荐
- 关于MySQL的使用记录
- redis学习备忘
- java 实验一Java开发环境(无脑实验系列)
- poj 1860 Currency Exchange
- H264 数据avi文件封装和拆解
- ThreadPoolExecutor里面4种拒绝策略(详细)
- Spring-data-redis:特性与实例
- 什么时候使用覆盖操作
- 正则表达式——肯定环视模拟固化分组
- #pragma_pack(n)_与__attribute(aligned(n))的关系与区别
- 享元模式
- Spark 定制版:006~Spark Streaming源码解读之Job动态生成和深度思考
- 继承,继承的好处
- struts-2小练习
- 2286: [Sdoi2011消耗战
- 工作队列
- 兄台息怒,关于arguments,您的想法和大神是一样一样的----闲聊JS中的apply和call
- android 简单的网格布局 GridLayout
- leetcode_c++:Combination Sum_2(040)
- Untiy3D的粒子系统介绍