xmlpullparser之skip(parser)

本文介绍了一种在XML解析过程中跳过不关心的标签的方法。通过递增和递减深度值来确保只跳过匹配的开始和结束标签对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 matching END_TAG.
  • To make sure that it stops at the correct END_TAG and not at the first tag it encounters after the original START_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 the START_TAG for <name>. The value for depth is incremented to 2.
  • The second time through the while loop, the next tag the parser encounters is the END_TAG </name>. The value for depth is decremented to 1.
  • The third time through the while loop, the next tag the parser encounters is the START_TAG <uri>. The value for depth is incremented to 2.
  • The fourth time through the while loop, the next tag the parser encounters is the END_TAG </uri>. The value for depth is decremented to 1.
  • The fifth time and final time through the while loop, the next tag the parser encounters is the END_TAG </author>. The value for depth 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,跳出循环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值