zoukankan      html  css  js  c++  java
  • xml解析

    public class MainActivity extends AppCompatActivity {
        private RequestQueue queues ;
    
        ImageView imageView;
        String xml = "<apps>
    " +
                "  <app>
    " +
                "    <id>1</id>
    " +
                "    <name>Google maps</name>
    " +
                "    <version>1.0</version>
    " +
                "  </app>
    " +
                "  <app>
    " +
                "     <id>2</id>
    " +
                "     <name>chrome</name>
    " +
                "     <version>2.1</version>
    " +
                "  </app>
    " +
                "  <app>
    " +
                "     <id>3</id>
    " +
                "     <name>google play</name>
    " +
                "     <version>3.1</version>
    " +
                "  </app> 
    " +
                "</apps>";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageView = (ImageView) findViewById(R.id.image);
            parseXMLWithPull(xml);
        }
        private void parseXMLWithPull(String xmlData){
            try {
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                XmlPullParser xmlPullParser = factory.newPullParser();
                //设置输入的内容
                xmlPullParser.setInput(new StringReader(xmlData));
                //获取当前解析事件,返回的是数字
                int eventType = xmlPullParser.getEventType();
                //保存内容
                String id = "";
                String name = "";
                String version="";
                while (eventType != (XmlPullParser.END_DOCUMENT)){
                    String nodeName = xmlPullParser.getName();
                    switch (eventType){
                        //开始解析XML
                        case XmlPullParser.START_TAG:{
                            //nextText()用于获取结点内的具体内容
                            if("id".equals(nodeName))
                                id = xmlPullParser.nextText();
                            else if("name".equals(nodeName))
                                name = xmlPullParser.nextText();
                            else if("version".equals(nodeName))
                                version = xmlPullParser.nextText();
                        } break;
                        //结束解析
                        case XmlPullParser.END_TAG:{
                            if("app".equals(nodeName)){
                                Log.d("输出:", "parseXMLWithPull: id is "+ id);
                                Log.d("输出:", "parseXMLWithPull: name is "+ name);
                                Log.d("输出:", "parseXMLWithPull: version is "+ version);
                            }
                        } break;
                        default: break;
                    }
                    //下一个
                    eventType = xmlPullParser.next();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    使用的是pull解析,安卓官方推荐的解析方法

  • 相关阅读:
    ZipHelper 压缩和解压帮助类
    搜索引擎索引数据结构和算法
    Api之Cors跨域以及其他跨域方式
    UML类图
    NetAnalyzer2016使用方法
    30分钟快速掌握AngularJs
    抖屏与收发各种类型文件
    NET Core RC2
    如何检测被锁住的Oracle存储过程及处理办法汇总(转)
    oracle spfile和pfile文件(转)
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10986790.html
Copyright © 2011-2022 走看看