刚才一直是在控制台输出。下面实现从xml文件中读取数据的方法。
1、添加一个 jar 包
xmlpull-1.1.3.1.jar
2、测试方法
@Test //将一个xml文件中的数据读取出来,并形成集合,集合中放的是city对象
public void test3() throws Exception{
XStream xstream = new XStream();
//起别名
xstream.alias("city", City.class);
xstream.alias("cities", List.class);
List<City> list = (List<City>) xstream.fromXML(new FileInputStream("src/cities.xml"));
for (City city : list) {
System.out.println(city.getName()+","+city.getDescription());
}
}
3、结果:控制台输出
上海,东方明珠
北京,国家的大脑