PullParserHelper操作工具类PullParserHelper
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PullParserHelper操作工具类PullParserHelper相关的知识,希望对你有一定的参考价值。
package com.ibm.bluestrategy.helper; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; public class PullParserHelper { public static List<Map<String, Object>> xmlPullParser(String xmlString, String tagName, String[] tagNameList) { List<Map<String, Object>> list = null; Map<String, Object> map = null; try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser pullParser = factory.newPullParser(); InputStream is = new ByteArrayInputStream( xmlString.getBytes("UTF-8")); pullParser.setInput(is, "utf-8"); int event = pullParser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { String nodeName = pullParser.getName(); switch (event) { case 0: list = new ArrayList<Map<String, Object>>(); break; case 2: if (nodeName.equals(tagName)) { map = new HashMap<String, Object>(); } for (int i = 0; i < tagNameList.length; i++) { if (nodeName.equals(tagNameList[i])) { map.put(tagNameList[i], pullParser.nextText()); } } break; case 3: if (tagName.equals(nodeName)) { list.add(map); } break; } event = pullParser.next(); } return list; } catch (Exception e) { e.printStackTrace(); } return null; } }
本文出自 “乐无莜” 博客,请务必保留此出处http://tiantiankaixin.blog.51cto.com/10762303/1869083
以上是关于PullParserHelper操作工具类PullParserHelper的主要内容,如果未能解决你的问题,请参考以下文章