xml解析代码示例
2024-08-04 20:59:24
List<Entry> list = new ArrayList<>();
Entry entry = null;
try {
int eventType = response.getEventType();
while(XmlPullParser.END_DOCUMENT != eventType){
switch (eventType){
case XmlPullParser.START_TAG:
String tag = response.getName();
if ("item".equalsIgnoreCase(tag)){
entry = new Entry(); }else if (entry != null){
if ("title".equalsIgnoreCase(tag)){
entry.setTitle(new String(response.nextText().getBytes(),"UTF-8"));
}else if ("description".equalsIgnoreCase(tag)){
entry.setDesc(new String(response.nextText().getBytes(),"UTF-8"));
}else if ("link".equalsIgnoreCase(tag)){
entry.setUrl(response.nextText());
}
}
break;
case XmlPullParser.END_TAG:
if (response.getName().equalsIgnoreCase("item") && entry != null) {
list.add(entry);
entry = null;
}
break;
default:
break;
}
eventType = response.next();
}
}catch (XmlPullParserException e) {
Log.e(TAG,"xml parse error :"+e.getLocalizedMessage());
} catch (IOException e) {
Log.e(TAG,"xml parse error IOException:"+e.getLocalizedMessage());
}
上面是第一种
try {
int eventType = response.getEventType();
while(XmlPullParser.END_DOCUMENT != eventType){
switch (eventType){
case XmlPullParser.START_TAG:
String tag = response.getName();
if ("entry".equalsIgnoreCase(tag)){
entry = new Entry(); }else if (entry != null){
if ("title".equalsIgnoreCase(tag)){
entry.setTitle(new String(response.nextText().getBytes(),"UTF-8"));
}else if ("content".equalsIgnoreCase(tag)){
entry.setDesc(new String(response.nextText().getBytes(),"UTF-8"));
}else if ("link".equalsIgnoreCase(tag)){
entry.setUrl(response.getAttributeValue(null, "href"));
}
}
break;
case XmlPullParser.END_TAG:
if (response.getName().equalsIgnoreCase("entry") && entry != null) {
list.add(entry);
entry = null;
}
break;
default:
break;
}
eventType = response.next();
}
}catch (XmlPullParserException e) {
Log.e(TAG,"xml parse error :"+e.getLocalizedMessage());
} catch (IOException e) {
Log.e(TAG,"xml parse error IOException:"+e.getLocalizedMessage());
}
上面是第二种
最新文章
- 对象比较器:Comparable和Comparator
- 关于JavaScript 中的变量
- knockout+bootstrap--一些复杂的应用合集
- HDU 4862 Jump(最小K路径覆盖)
- 蓝牙--对象交换协议(OBEX)
- JAVA学习中Swing概述中的JFrame学习
- Oracle GoldenGate 12c实时捕获SQL Server数据
- Cross-Browser HTML5 Placeholder Text
- tomcat 内存配置
- 核心概念 &mdash;&mdash; 服务提供者
- 暑假集训(4)第六弹——— 组合(poj1067)
- Xamarin android PreferenceActivity 实现应用程序首选项设置(一)
- PHP 时间函数集合
- 网站(Tomcat)超线程宕机
- Java缓存框架
- CodeForces 678A Johny Likes Numbers
- Visual Studio Team Services 帐户管理操作
- IOS safari浏览器登陆时Cookie无法保存的问题
- c++编译错误C2971:";std::array";:array_size:包含非静态存储不能用作废类型参数;参见“std::array”的声明
- tomcat的JVM调优