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


    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

  • 相关阅读:
    用ASP.Net2005和Oracle9i做模糊查询时报ORA01722错误解决方法
    ☠360与腾讯QQ的战争☠之我见
    【转】GIS原理学习
    【转】JavaScript写的Cookie类
    怀疑自己的笔记本电脑可能受到了网络攻击被人监视或操控
    “密码人”越来越多将会出现“密码危机”
    【转】确保 ASP.NET 应用程序和 Web Services 的安全
    超图软件(SuperMap)学习资料:桌面软件(Deskpro)
    用Sql Server 2000的数据库备份来还原Sql Server 2005中的数据库
    一个有用的Windows服务小程序——用来完成Server端的Socket通信
  • 原文地址:https://www.cnblogs.com/deman/p/4650544.html
Copyright © 2020-2023  润新知