标签:
Normally, there are three type parser in android. Xmlpullparser, DOM & SAX.
Google recomand Xmlpullparser to doing this.
But to parser xml files or inputstream, it based on xml content.
So using an general way to parser xml.
BaseXmlObj:
/*********************************************************** *@description : This class function is TODO * * @create author : deman.lu * @create date :2015-07-15 * @modify author : * @modify date : * @contact: joyfulmath.china@gmail.com * **********************************************************/ package com.joyfulmath.androidstudy.connect.xmlparser; import java.lang.reflect.Field; public abstract class BaseXmlObj { public abstract String[] getNodes(); public void setParamater(String tag, Object value) { try { Field field = getClass().getField(tag); field.setAccessible(true); field.set(this, value); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
/*********************************************************** *@description : This class function is TODO * * @create author : deman.lu * @create date :2015-07-15 * @modify author : * @modify date : * @contact: joyfulmath.china@gmail.com * **********************************************************/ package com.joyfulmath.androidstudy.connect.xmlparser; import java.io.InputStream; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.content.res.XmlResourceParser; public class XmlParserUtils { private static String encode = "utf-8"; public static XmlPullParser pullParser; static { try { pullParser = XmlPullParserFactory.newInstance().newPullParser(); } catch (XmlPullParserException e) { e.printStackTrace(); } } /** * * @description :解析节点中的内容,封装成对象模型。 * * @author : deman.lu * * @create :2015-07-15 * * @param in network data contains xml * * @param obj * * @throws Exception * * @return :T extends BaseXmlObj */ public static <T extends BaseXmlObj> void streamText2Model(InputStream in, T obj) throws Exception { pullParser.setInput(in, encode); int eventType = pullParser.getEventType(); String[] nodes = obj.getNodes(); String nodeName = null; boolean success = true; while (eventType != XmlPullParser.END_DOCUMENT && success) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: nodeName = pullParser.getName(); break; case XmlPullParser.TEXT: for (int i = 0; i < nodes.length; i++) { if (nodes[i].equals(nodeName)) { obj.setParamater(nodeName, pullParser.getText()); } } break; case XmlPullParser.END_TAG: break; } eventType = pullParser.next(); } } /** * * @description :解析节点中的内容,封装成对象模型。 * * @author : deman.lu * * @create :2015-07-15 * * @param resParser XmlResourceParser read form resource * * @param obj * * @throws Exception * * @return :void */ public static <T extends BaseXmlObj> void streamText2Model(XmlResourceParser resParser, T obj) throws Exception { int eventType = resParser.getEventType(); String[] nodes = obj.getNodes(); String nodeName = null; boolean success = true; while (eventType != XmlPullParser.END_DOCUMENT && success) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: nodeName = resParser.getName(); break; case XmlPullParser.TEXT: for (int i = 0; i < nodes.length; i++) { if (nodes[i].equals(nodeName)) { obj.setParamater(nodeName, resParser.getText()); } } break; case XmlPullParser.END_TAG: break; } eventType = resParser.next(); } } }
we using reflect, but if xml is complex, the efficiency may be low.
The way should be decided.
package com.joyfulmath.androidstudy.connect; import java.io.Serializable; import com.joyfulmath.androidstudy.connect.xmlparser.BaseXmlObj; public class Student extends BaseXmlObj implements Serializable { /** * */ private static final long serialVersionUID = 1L; public String GradeID, GradeName, ClassID, ClassName, UserID, UserName; public Student() { } @Override public String[] getNodes() { return new String[] { "GradeID", "GradeName", "ClassID", "ClassName", "UserID", "UserName" }; } @Override public String toString() { String su = "@student:"+"GradeID:"+GradeID +"GradeName:"+GradeName +"ClassID:"+ClassID +"ClassName:"+ClassName +"UserID:"+UserID +"UserName:"+UserName; return NetWorkUtils.replaceBlank(su); } }
package com.joyfulmath.androidstudy.connect; import com.joyfulmath.androidstudy.connect.xmlparser.XmlParserUtils; import android.content.Context; import android.content.res.XmlResourceParser; public class XmlParserDemo { public static Student parserStudentXml(Context context,int resId) { try { Student student = new Student(); XmlResourceParser xml = context.getResources() .getXml(resId); XmlParserUtils.streamText2Model(xml, student); xml.close(); return student; } catch (Exception e) { e.printStackTrace(); } return null; } }
<?xml version="1.0" encoding="UTF-8"?> <DsXml> <IsLog> true </IsLog> <GradeID> 10001 </GradeID> <GradeName> 高一年级 </GradeName> <ClassID> 10010 </ClassID> <ClassName> 高一(01)班 </ClassName> <UserID> 10000 </UserID> <UserName> 张三 </UserName> </DsXml>
转载:http://www.cnblogs.com/vanezkw/archive/2013/03/03/2941496.html
android network develop(3)----Xml Parser
标签:
原文地址:http://www.cnblogs.com/deman/p/4650544.html