xml解析工具类

前端之家收集整理的这篇文章主要介绍了xml解析工具类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参照:http://blog.csdn.net/nugongahou110/article/details/46963767点击打开链接


1:自定义javaBean:


  1. public class TerminalInit_Bean extends Base_Bean{
  2.  
  3. private String updateStatus;
  4. private String updateAddress;
  5. private String msgExt;
  6. private String downParam;
  7. private String respCode;
  8. private String respDesc;
  9. public String getRespCode() {
  10. return respCode;
  11. }
  12.  
  13. public void setRespCode(String respCode) {
  14. this.respCode = respCode;
  15. }
  16.  
  17. public String getRespDesc() {
  18. return respDesc;
  19. }
  20.  
  21. public void setRespDesc(String respDesc) {
  22. this.respDesc = respDesc;
  23. }
  24.  
  25. public String getUpdateStatus() {
  26. return updateStatus;
  27. }
  28.  
  29. public void setUpdateStatus(String updateStatus) {
  30. this.updateStatus = updateStatus;
  31. }
  32.  
  33. public String getUpdateAddress() {
  34. return updateAddress;
  35. }
  36.  
  37. public void setUpdateAddress(String updateAddress) {
  38. this.updateAddress = updateAddress;
  39. }
  40.  
  41. public String getMsgExt() {
  42. return msgExt;
  43. }
  44.  
  45. public void setMsgExt(String msgExt) {
  46. this.msgExt = msgExt;
  47. }
  48.  
  49. public String getDownParam() {
  50. return downParam;
  51. }
  52.  
  53. public void setDownParam(String downParam) {
  54. this.downParam = downParam;
  55. }
  56. }

2:使用方法

  1. Object obj = null;
  2. try {
  3. obj = XmlUtils.getBeanListByParseXml(new ByteArrayInputStream(Info.netResult.getBytes()), "upPay",cla);
  4. } catch (Exception e) {
  5. Info.cuowu = "xml报文解析失败";
  6. listener.onFail();
  7. }

3:工具类

  1. public class XmlUtils {
  2.  
  3. /** * 解析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)
  4. throws Exception {
  5.  
  6. XmlPullParser parser = Xml.newPullParser();
  7. //最后结果
  8. Object result = null;
  9. //list 存放一堆item
  10. ArrayList<T> list = null;
  11. //内层ListBean
  12. T t = null;
  13. //外层Bean
  14. T1 bean = null;
  15. //一个计数器
  16. int count = 0;
  17. Field mField = null; //List
  18. Field mFieldList = null;
  19. String listName = "";
  20. try {
  21. parser.setInput(inputStream,"UTF-8");
  22. //获得当前标签类型
  23. int eventType = parser.getEventType();
  24. //如果不是xml文件结束标签,则一个一个向下解析
  25. while (eventType != XmlPullParser.END_DOCUMENT) {
  26. switch (eventType) {
  27. //如果是xml文件开始标签,则初始化一些数据
  28. case XmlPullParser.START_DOCUMENT:
  29. //最后的结果
  30. result = new Object();
  31. //list
  32. list = new ArrayList<T>();
  33. break;
  34. //开始标签
  35. case XmlPullParser.START_TAG:
  36. //获得标签的名字
  37. String tagName = parser.getName();
  38. //如果内层的ListBean已经实例化出来的话
  39. if (t != null) {
  40. try {
  41. Field field = t.getClass().getDeclaredField(tagName);
  42. if (!listName.equals(tagName)) {
  43. //判断当前标签在没在ListBean属性
  44. if (!tagName.equals(listName)) {
  45. //如果ListBean中有当前标签
  46. if (field != null) {
  47. //计数器+1
  48. count++;
  49. //将取出来的值赋给ListBean中对应的属性
  50. field.setAccessible(true);
  51. field.set(t,parser.nextText());
  52. }
  53. }
  54. }
  55.  
  56. } catch (Exception e) {
  57. //如果ListBean中没有当前标签,则会直接跳到这里,什么都不执行,然后再继续往下走
  58. }
  59. //如果外层的Bean已经实例化出来的话
  60. } else if (bean != null) {
  61. try {
  62. //判断当前标签在没在Bean属性
  63. Field field = beanClazz.getDeclaredField(tagName);
  64. if (field.getType().getSimpleName().equals("List")) {
  65.  
  66. } else {
  67. //如果Bean中有当前标签
  68. if (field != null) {
  69. //计数器+1
  70. count++;
  71. //将取出来的值赋给Bean中对应的属性
  72. field.setAccessible(true);
  73. field.set(bean,parser.nextText());
  74. }
  75. }
  76. } catch (Exception e) {
  77. //如果Bean中没有当前标签,则会直接跳到这里,什么都不执行,然后再继续往下走
  78. }
  79. }
  80.  
  81. try {
  82. //判断当前标签类型是否为List
  83. mField = beanClazz.getDeclaredField(tagName);
  84. mField.setAccessible(true);
  85. if (mField.getType().getSimpleName().equals("List")) { //判断标签是否为 一个 List 集合
  86. Utils.log("listName = " + listName);
  87. Utils.log("tagName = " + tagName);
  88. if (!tagName.equals(listName)) { //判断是否为相同集合
  89. try {
  90. //如果不是相同一个集合,设置List
  91. mFieldList.set(bean,list);
  92. //清空List
  93. list.clear();
  94. } catch (Exception e) {
  95. //防止设置List 空指针异常
  96. }
  97. }
  98. mFieldList = mField;
  99. listName = tagName;
  100. //实例化List集合中 对象类型
  101. Class cla = (Class) (((ParameterizedType) mField.getGenericType())
  102. .getActualTypeArguments()[0]);
  103. t = (T) cla.newInstance();
  104. }
  105. } catch (Exception e) {
  106. }
  107.  
  108. //如果当前标签为我们传入的内层根标签,说明Bean需要实例化出来了
  109. if (tagName.equals(beanRoot)) {
  110. //Bean实例化出来
  111. bean = beanClazz.newInstance();
  112. }
  113. break;
  114. //结束标签
  115. case XmlPullParser.END_TAG:
  116. //如果当前标签</item>
  117. if (listName.equalsIgnoreCase(parser.getName())) {
  118. //如果ListBean不为空
  119. if (t != null) {
  120. //保存到list中,同时也保存到了result中,因为list已经是保存在result中了,
  121. //只不过刚才没有值,现在有值了
  122. list.add(t);
  123. //并且把ListBean置空,因为后续还有好多个item
  124. t = null;
  125. }
  126. } else if (beanRoot.equalsIgnoreCase(parser.getName())) { //最后结束标签
  127. try {
  128. //设置 list 防止空指针异常
  129. mFieldList.set(bean,list);
  130. } catch (Exception e) {
  131.  
  132. }
  133. //Bean保存到result
  134. result = bean;
  135. }
  136. break;
  137. }
  138. //移动到下一个标签
  139. eventType = parser.next();
  140. }
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144. //如果计数器为0说明没有解析到任何数据
  145. if (count == 0) {
  146. //result置空就可以了
  147. result = null;
  148. }
  149. //result返回
  150. return result;
  151. }
  152. }

猜你在找的XML相关文章