开发过程中,有时需要解析xml格式数据,但本人比较懒,不喜欢第次都写代码去解析不同的xml数据。所以写一个统一的解析方法去解析所有的xml数据。
如解析下面两段xml格式数据用统一的代码去处理:
<query xmlns='http://life.com/protocol/with' node='rooms'> <foo/> <goo jid='MyJID'/> <fun>fun text</fun> <item> <sub_item> <reason>reason text</reason> <item02 p='it'/> </sub_item> </item> </query>
<message name="room" age="21"> </message>
写一个统一的数据类型存放内容
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.text.TextUtils; /** * 元素 * @author Davee * @since 2014-08-19 */ public class Element { /** 属性 */ private Map<String,String> properties; /** 子元素 */ private List<Element> subEmelent; /** 元素名 */ private String elementName; /** 内容 */ private String body; public Element(String elementName) { this.elementName = elementName; } public String getElementName() { return elementName; } public void setBody(String body) { this.body = body; } public String getBody() { return body; } public void addProperty(String propertyName,String propertyValue) { if (properties == null) { properties = new HashMap<String,String>(); } properties.put(propertyName,propertyValue); } public String getProperty(String propertyName) { return properties.get(propertyName); } public Map<String,String> getProperties() { return properties; } public String removeProperty(String propertyName) { return properties.remove(propertyName); } public void addSubElement(Element subElement) { if (this.subEmelent == null) { this.subEmelent = new ArrayList<Element>(); } this.subEmelent.add(subElement); } public List<Element> getSubElements() { return subEmelent; } public boolean removeSubElement(Element subElement) { return this.subEmelent.remove(subElement); } /** * 返回XML格式 * @return */ public String toXml() { StringBuilder sb = new StringBuilder("<"); sb.append(elementName); if (properties != null) { for (Map.Entry<String,String> entry : properties.entrySet()) { sb.append(" ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\""); } } if (TextUtils.isEmpty(body) && subEmelent == null) { sb.append("/>"); } else { sb.append(">"); if (!TextUtils.isEmpty(body)) { sb.append(body); } if (subEmelent != null) { for (Element elem : subEmelent) { sb.append(elem.toXml()); } } sb.append("</").append(elementName).append(">"); } return sb.toString(); } /** * 返回Json格式 * @return */ public String toJson() { StringBuilder sb = new StringBuilder("{"); boolean needComma = false; if (!TextUtils.isEmpty(elementName)) { sb.append("\"element_name\":\"").append(elementName).append("\""); needComma = true; } if (properties != null && properties.size() > 0) { if (needComma) sb.append(","); sb.append("\"properties\":{"); needComma = false; for (Map.Entry<String,String> entry : properties.entrySet()) { if (!needComma) needComma = true; else if (needComma) sb.append(","); sb.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\""); } sb.append("}"); } if (!TextUtils.isEmpty(body)) { if (needComma) sb.append(","); sb.append("\"body=\":\"").append(body).append("\""); } if (subEmelent != null && subEmelent.size() > 0) { if (needComma) sb.append(","); sb.append("\"sub_element\":["); needComma = false; for (Element elem : subEmelent) { if (!needComma) needComma = true; else if (needComma) sb.append(","); sb.append(elem.toString()); } sb.append("]"); } sb.append("}"); return sb.toString(); } @Override public String toString() { return toXml(); } }
再写处理方法
import java.io.StringReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import android.util.SparseArray; public class XmlParser { private static SparseArray<Element> cache = new SparseArray<Element>(); public static Element parse(String xmlStr) { Element rootElement = null; StringReader input = null; try { XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false); input = new StringReader(xmlStr); parser.setInput(input); int eventType = parser.getEventType(); while(eventType != XmlPullParser.END_DOCUMENT) { boolean hasNext = false; if (eventType == XmlPullParser.START_TAG) { int curDepth = parser.getDepth(); Element curElement = new Element(parser.getName()); //设置元素属性 for (int i=0; i<parser.getAttributeCount(); i++) { curElement.addProperty(parser.getAttributeName(i),parser.getAttributeValue(i)); } if (!parser.isEmptyElementTag()) { eventType = parser.next(); if (eventType == XmlPullParser.TEXT) { //设置元素内容 curElement.setBody(parser.getText()); } else if (eventType == XmlPullParser.START_TAG) { hasNext = true; } } if (rootElement == null) { rootElement = curElement; cache.put(curDepth,curElement); } else { cache.put(curDepth,curElement); Element parentElement = cache.get(curDepth - 1); if (parentElement != null) parentElement.addSubElement(curElement); } } if (!hasNext) eventType = parser.next(); } } catch(Exception e) { e.printStackTrace(); } finally { if (input != null) { input.close(); } cache.clear(); } return rootElement; } }原文链接:/xml/297408.html