参照:http://blog.csdn.net/nugongahou110/article/details/46963767点击打开链接
1:自定义javaBean:
public class TerminalInit_Bean extends Base_Bean{ private String updateStatus; private String updateAddress; private String msgExt; private String downParam; private String respCode; private String respDesc; public String getRespCode() { return respCode; } public void setRespCode(String respCode) { this.respCode = respCode; } public String getRespDesc() { return respDesc; } public void setRespDesc(String respDesc) { this.respDesc = respDesc; } public String getUpdateStatus() { return updateStatus; } public void setUpdateStatus(String updateStatus) { this.updateStatus = updateStatus; } public String getUpdateAddress() { return updateAddress; } public void setUpdateAddress(String updateAddress) { this.updateAddress = updateAddress; } public String getMsgExt() { return msgExt; } public void setMsgExt(String msgExt) { this.msgExt = msgExt; } public String getDownParam() { return downParam; } public void setDownParam(String downParam) { this.downParam = downParam; } }
2:使用方法
Object obj = null; try { obj = XmlUtils.getBeanListByParseXml(new ByteArrayInputStream(Info.netResult.getBytes()), "upPay",cla); } catch (Exception e) { Info.cuowu = "xml报文解析失败"; listener.onFail(); }
3:工具类
public class XmlUtils { /** * 解析xml文件结构的方法, 返回一个对象 * * @param inputStream 解析内容 * @param beanRoot 外层Bean需要实例化对象的一个标识 * @param beanClazz Bean.class * @return 返回 object 对象 * @throws Exception */ public static <T,T1> Object getBeanListByParseXml(InputStream inputStream,String beanRoot,Class<T1> beanClazz) throws Exception { XmlPullParser parser = Xml.newPullParser(); @H_404_454@//最后结果 Object result = null; @H_404_454@//list 存放一堆@H_404_454@item @H_404_454@ ArrayList<T> list = null; @H_404_454@//内层@H_404_454@ListBean @H_404_454@ T t = null; @H_404_454@//外层@H_404_454@Bean @H_404_454@ T1 bean = null; @H_404_454@//一个计数器 int count = 0; Field mField = null; @H_404_454@//List @H_404_454@ @H_404_454@ Field mFieldList = null; String listName = ""; try { parser.setInput(inputStream,"UTF-8"); @H_404_454@//获得当前标签类型 int eventType = parser.getEventType(); @H_404_454@//如果不是@H_404_454@xml文件结束标签,则一个一个向下解析 while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { @H_404_454@//如果是@H_404_454@xml文件开始标签,则初始化一些数据 case XmlPullParser.START_DOCUMENT: @H_404_454@//最后的结果 result = new Object(); @H_404_454@//list @H_404_454@ list = new ArrayList<T>(); break; @H_404_454@//开始标签 case XmlPullParser.START_TAG: @H_404_454@//获得标签的名字 String tagName = parser.getName(); @H_404_454@//如果内层的@H_404_454@ListBean已经实例化出来的话 if (t != null) { try { Field field = t.getClass().getDeclaredField(tagName); if (!listName.equals(tagName)) { @H_404_454@//判断当前标签在没在@H_404_454@ListBean的属性中 if (!tagName.equals(listName)) { @H_404_454@//如果@H_404_454@ListBean中有当前标签 if (field != null) { @H_404_454@//计数器@H_404_454@+1 @H_404_454@ count++; @H_404_454@//将取出来的值赋给@H_404_454@ListBean中对应的属性 field.setAccessible(true); field.set(t,parser.nextText()); } } } } catch (Exception e) { @H_404_454@//如果@H_404_454@ListBean中没有当前标签,则会直接跳到这里,什么都不执行,然后再继续往下走 } @H_404_454@//如果外层的@H_404_454@Bean已经实例化出来的话 } else if (bean != null) { try { @H_404_454@//判断当前标签在没在@H_404_454@Bean的属性中 Field field = beanClazz.getDeclaredField(tagName); if (field.getType().getSimpleName().equals("List")) { } else { @H_404_454@//如果@H_404_454@Bean中有当前标签 if (field != null) { @H_404_454@//计数器@H_404_454@+1 @H_404_454@ count++; @H_404_454@//将取出来的值赋给@H_404_454@Bean中对应的属性 field.setAccessible(true); field.set(bean,parser.nextText()); } } } catch (Exception e) { @H_404_454@//如果@H_404_454@Bean中没有当前标签,则会直接跳到这里,什么都不执行,然后再继续往下走 } } try { @H_404_454@//判断当前标签类型是否为@H_404_454@List @H_404_454@ mField = beanClazz.getDeclaredField(tagName); mField.setAccessible(true); if (mField.getType().getSimpleName().equals("List")) { @H_404_454@//判断标签是否为 一个@H_404_454@ List 集合 Utils.log("listName = " + listName); Utils.log("tagName = " + tagName); if (!tagName.equals(listName)) { @H_404_454@//判断是否为相同集合 try { @H_404_454@//如果不是相同一个集合,设置@H_404_454@List值 mFieldList.set(bean,list); @H_404_454@//清空@H_404_454@List @H_404_454@ list.clear(); } catch (Exception e) { @H_404_454@//防止设置@H_404_454@List 空指针异常 } } mFieldList = mField; listName = tagName; @H_404_454@//实例化@H_404_454@List集合中 对象类型 Class cla = (Class) (((ParameterizedType) mField.getGenericType()) .getActualTypeArguments()[0]); t = (T) cla.newInstance(); } } catch (Exception e) { } @H_404_454@//如果当前标签为我们传入的内层根标签,说明@H_404_454@Bean需要实例化出来了 if (tagName.equals(beanRoot)) { @H_404_454@//将@H_404_454@Bean实例化出来 bean = beanClazz.newInstance(); } break; @H_404_454@//结束标签 case XmlPullParser.END_TAG: @H_404_454@//如果当前标签为@H_404_454@</item> @H_404_454@ if (listName.equalsIgnoreCase(parser.getName())) { @H_404_454@//如果@H_404_454@ListBean不为空 if (t != null) { @H_404_454@//保存到@H_404_454@list中,同时也保存到了@H_404_454@result中,因为@H_404_454@list已经是保存在@H_404_454@result中了, @H_404_454@//只不过刚才没有值,现在有值了 list.add(t); @H_404_454@//并且把@H_404_454@ListBean置空,因为后续还有好多个@H_404_454@item @H_404_454@ t = null; } } else if (beanRoot.equalsIgnoreCase(parser.getName())) { @H_404_454@//最后结束标签 try { @H_404_454@//设置@H_404_454@ list 防止空指针异常 mFieldList.set(bean,list); } catch (Exception e) { } @H_404_454@//将@H_404_454@Bean保存到@H_404_454@result中 result = bean; } break; } @H_404_454@//移动到下一个标签 eventType = parser.next(); } } catch (Exception e) { e.printStackTrace(); } @H_404_454@//如果计数器为@H_404_454@0说明没有解析到任何数据 if (count == 0) { @H_404_454@//将@H_404_454@result置空就可以了 result = null; } @H_404_454@//将@H_404_454@result返回 return result; } }