XmlReader implements Closeable { public void skipRemainingElement() throws IOException { int stackPeek = stack[stackSize - 1]; if (stackPeek != XmlScope.ELEMENT_OPENING && stackPeek != XmlScope.ELEMENT_ATTRIBUTE) { throw new AssertionError( "This method can only be invoked after having consumed the opening element via beginElement()"); } int count = 1; do { switch (peek()) { case ELEMENT_BEGIN: beginElement(); count++; break; case ELEMENT_END: endElement(); count--; break; case ELEMENT_NAME: nextElementName(); break; case ATTRIBUTE_NAME: nextAttributeName(); break; case ATTRIBUTE_VALUE: skipAttributeValue(); break; case ELEMENT_TEXT_CONTENT: skipTextContent(); break; case END_OF_DOCUMENT: if (count != 0) { throw syntaxError("Unexpected end of file! At least one xml element is not closed!"); } break; default: throw new AssertionError( "Oops, there is something not implemented correctly internally. Please fill an issue on https: } peeked = PEEKED_NONE; } while (count != 0); } private  XmlReader(BufferedSource source); static XmlReader of(BufferedSource source); XmlToken peek(); void beginElement(); void endElement(); boolean hasElement(); boolean hasAttribute(); String nextAttributeName(); String nextAttributeValue(); int nextAttributeValueAsInt(); long nextAttributeValueAsLong(); boolean nextAttributeValueAsBoolean(); double nextAttributeValueAsDouble(); void skipAttributeValue(); void skipAttribute(); boolean hasTextContent(); String nextTextContent(); int nextTextContentAsInt(); long nextTextContentAsLong(); double nextTextContentAsDouble(); boolean nextTextContentAsBoolean(); void skipTextContent(); String getPath(); @Override void close(); String nextElementName(); String getCurrentElementName(); void skipRemainingElement();  }
@Test public void skipRemainingElement() throws IOException { String xml = "<foo><e1></e1><bar a='1' b='2'>TextContent<child a='1'>Child text Value<other b='123'>Text<inline /></other></child> <![CDATA[some <cdata></> &]]> </bar>TextAfterSkippedElement<element></element></foo>"; XmlReader reader = readerFrom(xml); try { Assert.assertTrue(reader.hasElement()); reader.beginElement(); Assert.assertEquals("foo", reader.nextElementName()); reader.beginElement(); Assert.assertEquals("e1", reader.nextElementName()); reader.endElement(); reader.beginElement(); reader.skipRemainingElement(); Assert.assertEquals("TextAfterSkippedElement", reader.nextTextContent()); reader.beginElement(); Assert.assertEquals("element", reader.nextElementName()); reader.endElement(); reader.endElement(); } finally { reader.close(); } }