Java XMLStreamReader skip(XMLStreamReader parser)

Here you can find the source of skip(XMLStreamReader parser)

Description

Skips a tag or subtree.

License

Apache License

Declaration

public static void skip(XMLStreamReader parser) throws XMLStreamException 

Method Source Code

//package com.java2s;
/*/*from w w w  .j av  a 2  s . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class Main {
    /**
     * Skips a tag or subtree.
     */
    public static void skip(XMLStreamReader parser) throws XMLStreamException {
        assert parser != null;

        int level = 1;
        while (next(parser)) {
            int event = parser.getEventType();
            if (event == XMLStreamConstants.START_ELEMENT) {
                level++;
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                level--;
                if (level == 0) {
                    break;
                }
            }
        }

        next(parser);
    }

    /**
     * Moves the parser to the next element.
     */
    public static boolean next(XMLStreamReader parser) throws XMLStreamException {
        assert parser != null;

        if (parser.hasNext()) {
            parser.next();
            return true;
        }

        return false;
    }
}

Related

  1. printLiterally(final PrintStream os, final XMLStreamReader xmlReader)
  2. printText(XMLStreamReader xmlr)
  3. readEndTag(XMLStreamReader reader, String localName)
  4. readEvent(XMLStreamReader reader)
  5. readTextWithNodes(XMLStreamReader xsr, Map nodeIdToOffsetMap)
  6. skip(XMLStreamReader parser)
  7. skipEventsTo(int targetEvent, XMLStreamReader parser)
  8. skipNewLines(XMLStreamReader xmlStreamReader)
  9. toReadableEvent(XMLStreamReader r)