前两天帮一个朋友做xml的解析,因为以前也也没怎么做过所以就百度了下
需要解析的xml文件名是 “3151_01_0001.xml”
这是其中一些节点:
每个大的节点都是单独定义的对象,如图
下面是解析代码:
try {
File file = new File("C:\\xml\\3151_01_0001.xml");
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String re = "";
while( (null != (re = reader.readLine()))) {
sb.append(re);
}
System.out.println(sb.toString());
XStream xstream = new XStream();
xstream.processAnnotations(TranData.class);
TranData mTranData = (TranData) xstream.fromXML(sb.toString());
System.out.println(mTranData.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
其中”C:\\xml\\3151_01_0001.xml“是要解析的xml文件所在路径
解析结果如下图:
资源下载地址:
http://download.csdn.net/detail/android_amelia/9410944