android下通过xstream解析xml格式信息

2024-06-06 03:58

本文主要是介绍android下通过xstream解析xml格式信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

==========推荐============

 实例教程-----会员贡献索引贴

http://www.eoeandroid.com/thread-1987-1-1.html

 

android 图像处理滤镜系列合集

http://www.eoeandroid.com/thread-178656-1-1.html

分享45个android实例源码

http://www.eoeandroid.com/thread-185986-1-1.html

==========帖子正文==========

可以通过json格式向android
http客户端传输数据,见:android下支持json的远程访问,也可以用xml格式。
  下面是一个xml文件的格式示例。

<product>  
<name>NetGear 614v9无线路由器</name>  
<createTime>2009-10-27 00:00:00.0 CST</createTime>  
</product>

下载或者访问该xml文件:[Download not found]
  如果解析上面的xml文件呢?这里选用了xstream,网址:

http://xstream.codehaus.org/

xstream可以自动解析文件,并且根据xml数据实例化javabean。如果不这样,需要手工编写SAX
API代码解析。
  首先编写了一个对应的Product的javabean:

package com.easymorse;  
import java.util.Date;  
public class Product { 
@Override
public String toString() { 
return “Product [createTime=" + createTime.toLocaleString() + ", name=" + name + "]“; 
}  
private String name;  
public String getName() { 
return name; 
}  
public void setName(String name) { 
this.name = name; 
}  
public Date getCreateTime() { 
return createTime; 
}  
public void setCreateTime(Date createTime) { 
this.createTime = createTime; 
}  
private Date createTime; 
}

然后,需要类似这样调用xstream的代码(代码还是改自实现android activity之间的跳转):

package com.easymorse;  
import java.io.BufferedReader; 
import java.io.InputStreamReader;  
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient;  
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView;  
import com.thoughtworks.xstream.XStream;  
public class NextActivity extends Activity { 
private TextView textView; 
@Override
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
this.setContentView(R.layout.next_activity); 
this.textView=(TextView) this.findViewById(R.id.TextView01);  
HttpClient client = new DefaultHttpClient(); 
StringBuilder builder = new StringBuilder();  
HttpGet get = new HttpGet( 
“http://marshal.easymorse.com/wp-content/uploads/2009/10/product2.xml”); 
try { 
HttpResponse response = client.execute(get); 
BufferedReader reader = new BufferedReader(new InputStreamReader( 
response.getEntity().getContent())); 
for (String s = reader.readLine(); s != null; s = reader.readLine()) { 
builder.append(s); 
} 
Log.v(“response”,”product:”+builder.toString()); 
XStream xstream = new XStream(); 
xstream.alias(“product”, Product.class); 
Product product=(Product) xstream.fromXML(builder.toString()); 
this.textView.setText(product.toString()); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
}

执行NextActivity的截图:

从服务器端返回的中文内容能够正确解码。不过,如果通过eclipse插件中的ddms日志,看到的是乱码,估计和日志或者eclipse插件默认字符集有关 。

另外,想要使用xstream需要引入xstream包。具体方法见:在eclipse的android项目中引入第三方包。在这里xstream又依赖xpp3用于对xml解析。xpp3的网址:

http://www.extreme.indiana.edu/xgws/xsoap/xpp/

可以在这里下载到最新的xpp3分发包:

 http://www.extreme.indiana.edu/dist/java-repository/xpp3/distributions/

然后解压缩,将其中的xpp3_min-*.jar导入项目即可。
  或者也可以选择不依赖xpp3包,这样可以节省24K左右的空间。需要实例化XStream时:

XStream xstream = new XStream(new DomDriver());

另外,日期格式用:

2012-07-23 00:00:00.0 CST

是为了直接转型方面,如果比较复杂,需要实现xstream的转型接口做定制实现:

 http://xstream.codehaus.org/converter-tutorial.html

 

 

这篇关于android下通过xstream解析xml格式信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035025

相关文章

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分