SimpleXMLEventReader implements XMLEventReader { @Override public XMLEvent nextTag() throws XMLStreamException { if (peekedEvent == null) { currentEventType = delegate.nextTag(); if (currentEventType != XMLStreamConstants.START_ELEMENT && currentEventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException("Expected start element event or end element event"); } return allocate(); } currentEventType = peekedEvent.getEventType(); assert currentEventType == delegate.getEventType(); XMLEvent event = peekedEvent; peekedEvent = null; switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: break; case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.SPACE: break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: if (!event.asCharacters().isWhiteSpace()) { throw new XMLStreamException("Encountered non-whitespace text"); } break; case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.END_ELEMENT: return event; default: throw new XMLStreamException("Encountered unexpected event: " + event.getEventType()); } while (true) { currentEventType = delegate.next(); switch (currentEventType) { case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.SPACE: continue; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: if (!delegate.isWhiteSpace()) { throw new XMLStreamException("Encountered non-whitespace text"); } continue; case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.END_ELEMENT: return allocate(); default: throw new XMLStreamException("Encountered unexpected event: " + delegate.getEventType()); } } } SimpleXMLEventReader(XMLStreamReader delegate);  SimpleXMLEventReader(XMLStreamReader delegate, XMLEventAllocator allocator); @Override void close(); @Override String getElementText(); @Override Object getProperty(String name); @Override boolean hasNext(); @Override XMLEvent nextEvent(); @Override Object next(); @Override XMLEvent nextTag(); @Override XMLEvent peek(); @Override void remove();  }
@Test public void testNextTag() throws XMLStreamException { XMLEventReader reader = createXmlEventReader("<?xml version=\"1.0\"?><alice david=\"edgar\"><edgar/></alice>"); Assert.assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag().getEventType()); Assert.assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag().getEventType()); Assert.assertEquals(XMLStreamConstants.END_ELEMENT, reader.nextTag().getEventType()); Assert.assertEquals(XMLStreamConstants.END_ELEMENT, reader.nextTag().getEventType()); reader = createXmlEventReader("<?xml version=\"1.0\"?><alice david=\"edgar\"><edgar/></alice>"); reader.peek(); Assert.assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag().getEventType()); reader.peek(); Assert.assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag().getEventType()); reader.peek(); Assert.assertEquals(XMLStreamConstants.END_ELEMENT, reader.nextTag().getEventType()); reader.peek(); Assert.assertEquals(XMLStreamConstants.END_ELEMENT, reader.nextTag().getEventType()); }