在xmlpullparser中,当您多次调用getEventType时,值会更改

时间:2014-03-20 22:22:25

标签: android xmlpullparser

当我多次跑步时。 我知道“xpp.next()”的值已经改变了。

但是,我不明白“xpp.getEventType()”的值也会改变。

当你进入调试器时,发现值发生了变化。

如果不是Break,则值不会改变。

为什么价值会改变?

       package com.example.xmlpulltestapp;

   import java.io.IOException;
   import java.io.StringReader;

   import org.xmlpull.v1.XmlPullParser;
   import org.xmlpull.v1.XmlPullParserException;
   import org.xmlpull.v1.XmlPullParserFactory;

   public class SimpleXmlPull
   {
        public static void main() throws XmlPullParserException, IOException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput(new StringReader(data));

        int event = 0;
        int cnt = 0;
        event = xpp.getEventType(); log(event, cnt++); // 0 Eclipse Break
        event = xpp.getEventType(); log(event, cnt++); // 1
        event = xpp.getEventType(); log(event, cnt++); // 2
        event = xpp.getEventType(); log(event, cnt++); // 3
        event = xpp.getEventType(); log(event, cnt++); // 4
        event = xpp.getEventType(); log(event, cnt++); // 5
        event = xpp.getEventType(); log(event, cnt++); // 6
        event = xpp.getEventType(); log(event, cnt++); // 7
        event = xpp.getEventType(); log(event, cnt++); // 8
        event = xpp.getEventType(); log(event, cnt++); // 9
        event = xpp.getEventType(); log(event, cnt++); // 10
        event = xpp.getEventType(); log(event, cnt++); // 11
        event = xpp.getEventType(); log(event, cnt++); // 12
        event = xpp.getEventType(); log(event, cnt++); // 13
        event = xpp.getEventType(); log(event, cnt++); // 14
        event = xpp.getEventType(); log(event, cnt++); // 15
        event = xpp.getEventType(); log(event, cnt++); // 16
        event = xpp.getEventType(); log(event, cnt++); // 17
        event = xpp.getEventType(); log(event, cnt++); // 18
        event = xpp.getEventType(); log(event, cnt++); // 19
        event = xpp.getEventType(); log(event, cnt++); // 20
        event = xpp.getEventType(); log(event, cnt++); // 21
        event = xpp.getEventType(); log(event, cnt++); // 22
        event = xpp.getEventType(); log(event, cnt++); // 23
        event = xpp.getEventType(); log(event, cnt++); // 24
        event = xpp.getEventType(); log(event, cnt++); // 25
        event = xpp.getEventType(); log(event, cnt++); // 26
        event = xpp.getEventType(); log(event, cnt++); // 27
        event = xpp.getEventType(); log(event, cnt++); // 28
        event = xpp.getEventType(); log(event, cnt++); // 29
        event = xpp.getEventType(); log(event, cnt++); // 30
    }

    static String data ="<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
                + "<ttFont sfntVersion=\"\\x00\\x01\\x00\\x00\" ttLibVersion=\"2.4\">"
                +   "<cmap>"
                +       "<tableVersion version=\"0\"/>"
                +       "<cmap_format_4 platformID=\"0\" platEncID=\"3\" language=\"0\">"
                +           "<map code=\"0x20\" name=\"space\"/>"
                +           "<map code=\"0x21\" name=\"exclam\"/>"
                +           "<map code=\"0x22\" name=\"quotedbl\"/>"
                +           "<map code=\"0x23\" name=\"numbersign\"/>"
                +           "<map code=\"0x24\" name=\"dollar\"/>"
                +       "</cmap_format_4>"
                +       "<cmap_format_6 platformID=\"1\" platEncID=\"0\" language=\"0\">"
                +           "<map code=\"0x20\" name=\"space\"/>"
                +           "<map code=\"0x21\" name=\"exclam\"/>"
                +           "<map code=\"0x22\" name=\"quotedbl\"/>"
                +           "<map code=\"0x23\" name=\"numbersign\"/>"
                +           "<map code=\"0x24\" name=\"dollar\"/>"
                +       "</cmap_format_6>"
                +       "<cmap_format_4 platformID=\"3\" platEncID=\"1\" language=\"0\">"
                +           "<map code=\"0x20\" name=\"space\"/>"
                +           "<map code=\"0x21\" name=\"exclam\"/>"
                +           "<map code=\"0x22\" name=\"quotedbl\"/>"
                +           "<map code=\"0x23\" name=\"numbersign\"/>"
                +           "<map code=\"0x24\" name=\"dollar\"/>"
                +       "</cmap_format_4>"
                +   "</cmap>"
                + "</ttFont>";


  }

这是您在Eclipse中进入Break时的日志。 eventtype的值会改变,但不会运行下一个。

03-22 09:35:06.043: I/System.out(3548): EventType=2 Cnt=0
03-22 09:35:06.643: I/System.out(3548): EventType=2 Cnt=1
03-22 09:35:07.223: I/System.out(3548): EventType=3 Cnt=2
03-22 09:35:07.823: I/System.out(3548): EventType=2 Cnt=3
03-22 09:35:08.303: I/System.out(3548): EventType=2 Cnt=4
03-22 09:35:08.803: I/System.out(3548): EventType=3 Cnt=5
03-22 09:35:09.303: I/System.out(3548): EventType=2 Cnt=6
03-22 09:35:09.803: I/System.out(3548): EventType=3 Cnt=7
03-22 09:35:10.293: I/System.out(3548): EventType=2 Cnt=8
03-22 09:35:10.773: I/System.out(3548): EventType=3 Cnt=9
03-22 09:35:11.233: I/System.out(3548): EventType=2 Cnt=10
03-22 09:35:11.663: I/System.out(3548): EventType=3 Cnt=11
03-22 09:35:12.123: I/System.out(3548): EventType=2 Cnt=12
03-22 09:35:12.613: I/System.out(3548): EventType=3 Cnt=13
03-22 09:35:13.103: I/System.out(3548): EventType=3 Cnt=14
03-22 09:35:13.563: I/System.out(3548): EventType=2 Cnt=15
03-22 09:35:14.083: I/System.out(3548): EventType=2 Cnt=16
03-22 09:35:14.623: I/System.out(3548): EventType=3 Cnt=17
03-22 09:35:15.053: I/System.out(3548): EventType=2 Cnt=18
03-22 09:35:15.533: I/System.out(3548): EventType=3 Cnt=19
03-22 09:35:16.003: I/System.out(3548): EventType=2 Cnt=20
03-22 09:35:16.483: I/System.out(3548): EventType=3 Cnt=21
03-22 09:35:17.433: I/System.out(3548): EventType=2 Cnt=22
03-22 09:35:17.903: I/System.out(3548): EventType=3 Cnt=23
03-22 09:35:18.353: I/System.out(3548): EventType=2 Cnt=24
03-22 09:35:18.833: I/System.out(3548): EventType=3 Cnt=25
03-22 09:35:19.323: I/System.out(3548): EventType=3 Cnt=26
03-22 09:35:19.853: I/System.out(3548): EventType=2 Cnt=27
03-22 09:35:20.703: I/System.out(3548): EventType=2 Cnt=28
03-22 09:35:21.903: I/System.out(3548): EventType=3 Cnt=29
03-22 09:35:22.723: I/System.out(3548): EventType=2 Cnt=30

这是您不执行步骤的日志。 eventType的值不会改变。

03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=0
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=1
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=2
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=3
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=4
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=5
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=6
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=7
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=8
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=9
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=10
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=11
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=12
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=13
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=14
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=15
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=16
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=17
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=18
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=19
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=20
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=21
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=22
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=23
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=24
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=25
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=26
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=27
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=28
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=29
03-22 09:35:52.473: I/System.out(3605): EventType=2 Cnt=30

1 个答案:

答案 0 :(得分:0)

getEventType()方法返回一个静态变量,具体取决于它当前正在解析的XML部分。 next()方法也是如此,它与getEventType()方法基本相同,可以解释为什么值会发生变化。它不打算以您实现它的方式多次调用。从以下代码中可以看出,您可以根据getEventType() / next()方法返回值切换操作:

int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) { // Keep iterating unless the current eventType is the end of the document
    if(eventType == XmlPullParser.START_DOCUMENT) { // Switch action based on the current eventType value
        System.out.println("Start document");
    } else if(eventType == XmlPullParser.START_TAG) {
        System.out.println("Start tag "+xpp.getName());
    } else if(eventType == XmlPullParser.END_TAG) {
        System.out.println("End tag "+xpp.getName());
    } else if(eventType == XmlPullParser.TEXT) {
        System.out.println("Text "+xpp.getText());
    }
    eventType = xpp.next(); // Set the current event type from the return value of next()
}