xml解析

public class MainActivity extends AppCompatActivity {
    private RequestQueue queues ;

    ImageView imageView;
    String xml = "<apps>\n" +
            "  <app>\n" +
            "    <id>1</id>\n" +
            "    <name>Google maps</name>\n" +
            "    <version>1.0</version>\n" +
            "  </app>\n" +
            "  <app>\n" +
            "     <id>2</id>\n" +
            "     <name>chrome</name>\n" +
            "     <version>2.1</version>\n" +
            "  </app>\n" +
            "  <app>\n" +
            "     <id>3</id>\n" +
            "     <name>google play</name>\n" +
            "     <version>3.1</version>\n" +
            "  </app> \n" +
            "</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解析,安卓官方推荐的解析方法

转载于:https://www.cnblogs.com/Ocean123123/p/10986790.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值