Android XmlPullParser解析类似的节点

时间:2013-12-29 04:59:29

标签: android xml xml-parsing xmlpullparser

我有几个与我需要解析的xml节点相同:

<start-valid-time period-name="Overnight">2013-12-29T00:00:00-05:00</start-valid-time>
<start-valid-time period-name="Sunday">2013-12-29T06:00:00-05:00</start-valid-time>
<start-valid-time period-name="Sunday Night">2013-12-29T18:00:00-05:00</start-valid-time>
<start-valid-time period-name="Monday">2013-12-30T06:00:00-05:00</start-valid-time>

我的代码:

if(parser.getName().equalsIgnoreCase("start-valid-time")) {
   periodnames[0] = parser.getAttributeValue(0);
}

我得到了第一个period-name属性,但如果我试试这个:

if(parser.getName().equalsIgnoreCase("start-valid-time")) {
   periodnames[0] = parser.getAttributeValue(0);
   parser.nextTag();
   periodnames[1] = parser.getAttributeValue(0);
}

它不收集第二个时期名称,在这种情况下是星期日......

如果有人可以提供帮助,我们将不胜感激.-。

1 个答案:

答案 0 :(得分:0)

XML

<?xml version="1.0" ?>
<start-valid-time period-name="Overnight">2013-12-29T00:00:00-05:00</start-valid-time>
<start-valid-time period-name="Sunday">2013-12-29T06:00:00-05:00</start-valid-time>
<start-valid-time period-name="Sunday Night">2013-12-29T18:00:00-05:00</start-valid-time>
<start-valid-time period-name="Monday">2013-12-30T06:00:00-05:00</start-valid-time>

解析并获取属性

测试我把xml放在assests文件夹中。 inInputStream

 InputStream in = MainActivity.this.getResources()
                 .getAssets().open("xmlparser.xml");
 try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            readFeed(parser);
        } catch(Exeption e)
            {
                 e.prinstStacktrace();
            }finally {
            in.close();
        }

然后

  private void readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
            String name=null,value;
            while (parser.getEventType() == XmlPullParser.START_TAG && (parser.getEventType() != XmlPullParser.END_TAG)) {
                   value = parser.getAttributeValue(null, "period-name");
                    name = readText(parser);
                    Log.i("Name.....",name);
                    Log.i("Value ...",value);
                    parser.next();
                    continue;
            }  

        }

READTEXT

  private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

日志

12-29 03:01:23.433: I/Name.....(3273): 2013-12-29T00:00:00-05:00
12-29 03:01:23.433: I/Value ...(3273): Overnight
12-29 03:01:23.433: I/Name.....(3273): 2013-12-29T06:00:00-05:00
12-29 03:01:23.443: I/Value ...(3273): Sunday
12-29 03:01:23.443: I/Name.....(3273): 2013-12-29T18:00:00-05:00
12-29 03:01:23.443: I/Value ...(3273): Sunday Night
12-29 03:01:23.443: I/Name.....(3273): 2013-12-30T06:00:00-05:00
12-29 03:01:23.443: I/Value ...(3273): Monday