Skip Tags You Don't Care About
One of the steps in the XML parsing described above is for the parser to skip tags it's not interested in. Here is the parser's skip()
method:
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { if (parser.getEventType() != XmlPullParser.START_TAG) { throw new IllegalStateException(); } int depth = 1; while (depth != 0) { switch (parser.next()) { case XmlPullParser.END_TAG: depth--; break; case XmlPullParser.START_TAG: depth++; break; } }
This is how it works:
- It throws an exception if the current event isn't a
START_TAG
. - It consumes the
START_TAG
, and all events up to and including the matchingEND_TAG
. - To make sure that it stops at the correct
END_TAG
and not at the first tag it encounters after the originalSTART_TAG
, it keeps track of the nesting depth.
Thus if the current element has nested elements, the value of depth
won't be 0 until the parser has consumed all events between the originalSTART_TAG
and its matching END_TAG
. For example, consider
how the parser skips the <author>
element, which has 2 nested elements, <name>
and<uri>
:
- The first time through the
while
loop, the next tag the parser encounters after<author>
is theSTART_TAG
for<name>
. The value fordepth
is incremented to 2. - The second time through the
while
loop, the next tag the parser encounters is theEND_TAG
</name>
. The value fordepth
is decremented to 1. - The third time through the
while
loop, the next tag the parser encounters is theSTART_TAG
<uri>
. The value fordepth
is incremented to 2. - The fourth time through the
while
loop, the next tag the parser encounters is theEND_TAG
</uri>
. The value fordepth
is decremented to 1. - The fifth time and final time through the
while
loop, the next tag the parser encounters is theEND_TAG
</author>
. The value fordepth
is decremented to 0, indicating that the<author>
element has been successfully skipped。 - 以上是官网给出的 在解析xml文件,跳过某些标签的方法。
- 工作原理那三点不易理解,但是从给出的一个关于<Author>标签 解析的例子参照来看,理解3点就很容易了。
- <Author/>进入该标签后,进行第一次循环,遇到的开始标签是<name>,depth值加一。(此处为什么depth初始值是1,因为解析读入Author标签了,当然为一,此处设置为一,只是为了配合判断条件为0来的)。
- 以下的意思就是,遇到开始标签,depth值加一,遇到结束标签,depth值减一。最后为0,跳出循环。