Filter text while (after) parsing with XMLPullParser

别等时光非礼了梦想. 提交于 2020-03-16 09:56:58

问题


I have some xml like:

<tag attr1="20190325235500 +0200">

</tag>
<tag attr1="20190326000000 +0200">

</tag>
<tag attr1="20190326000000 +0200">

</tag>
<tag attr1="20190326000000 +0200">
</tag>

I parse it with XMLPullParser like:

                if (parser.getName().equals(tag)) {
                    attr1 = parser.getAttributeValue(0);
                    item = new SomeItem(attr1);
                    item.setAttr1Name(attr1);
                    items.add(item);

                }

So I have a lot of records like attr1="20190326000000 +0200"

Now I want to filter all this records and leave only those which are starting 20190326, for example.

I thought this would help me:

if (parser.getName().equals(tag) && parser.getAttributeValue(0).substring(0, 8).equals("20190326"))

but I was wrong and this if causing nullpointer exception in my item.setAttr1Name(attr1);

What I can try to do? How to build right if?

Maybe I should use something of Date, Calendar, DateFormat?..


回答1:


Try this

if (parser.getName().equals(tag)) {
    attr1 = parser.getAttributeValue(0);
    if (attr1 != null && attr1.startsWith("20190326") {
        item = new SomeItem(attr1);
        item.setAttr1Name(attr1);
        items.add(item);
    }
}


来源:https://stackoverflow.com/questions/55827684/filter-text-while-after-parsing-with-xmlpullparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!