Android XmlPullParser解析xml(2)

(1)在main路径下,创建assets目录,存放待解析的xml文件.

如:/app/src/main/assets/app_list.xml

<?xml version="1.0" encoding="utf-8"?>
<applist enable="true">
    <package name="com.tencent.mm" intime="false"/>
    <package name="com.ss.android.article.news" intime="true"/>
    <package name="com.netease.cloudmusic" intime="true"/>
    <package name="com.moji.mjweather" intime="true"/>
    <package name="cn.wps.moffice_eng" intime="false"/>
</applist>

(2)创建对象,存放解析出来的信息.

 public class AppInfo {
        private String name;
        private boolean intime;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setIntime(boolean intime) {
            this.intime = intime;
        }

        public boolean getIntime() {
            return intime;
        }

    @Override
    public String toString() {
        return "AppInfo{" +
                "name='" + name + '\'' +
                ", intime=" + intime +
                '}';
    }
}

(3)解析数据

public class MainActivity extends AppCompatActivity {

    private List<AppInfo> appInfoList = new ArrayList<>();
    private boolean enable = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            parse();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void parse() throws XmlPullParserException, IOException {

         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         XmlPullParser pullParser = factory.newPullParser();
         InputStream inputStream = getAssets().open("app_list.xml");
         pullParser.setInput(inputStream, null);

        int eventType = pullParser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {

            eventType = pullParser.getEventType();
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    String tagName = pullParser.getName();
                    if (tagName != null && tagName.equals("package")) {
                        AppInfo  appInfo= new AppInfo();
                        String name = pullParser.getAttributeValue("", "name");
                        boolean isIntime = Boolean.parseBoolean(pullParser.getAttributeValue("", "intime"));
                        appInfo.setName(name);
                        appInfo.setIntime(isIntime);
                        appInfoList.add(appInfo);
                    } else if (tagName.equals("applist")) { 
                         if("false".equals(pullParser.getAttributeValue(null,"enable"))){
                              enable = false;
                         }
                         }
                    break;
                default:
                    break;
            }
            eventType = pullParser.next();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值