android network develop(3)----Xml Parser

來源:互聯網
上載者:User

標籤:

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: [email protected] * **********************************************************/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: [email protected] * **********************************************************/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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.