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,跳出循環。