SimpleXMLEventReader implements XMLEventReader { @Override public String getElementText() throws XMLStreamException { if (currentEventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Expected start element event"); } if (peekedEvent == null) { String result = delegate.getElementText(); currentEventType = delegate.getEventType(); if (currentEventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException("Expected end element event"); } return result; } currentEventType = peekedEvent.getEventType(); assert currentEventType == delegate.getEventType(); peekedEvent = null; String leadText = null; switch (currentEventType) { case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.ENTITY_REFERENCE: leadText = delegate.getText(); break; case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.SPACE: break; case XMLStreamConstants.END_ELEMENT: return ""; default: throw new XMLStreamException("Unexpected event type " + currentEventType, delegate.getLocation()); } StringBuilder builder = null; while (true) { currentEventType = delegate.next(); switch (currentEventType) { case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.ENTITY_REFERENCE: if (leadText == null) { leadText = delegate.getText(); } else { if (builder == null) { builder = new StringBuilder(leadText); } builder.append(delegate.getText()); } break; case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.SPACE: break; case XMLStreamConstants.END_ELEMENT: return builder == null ? (leadText == null ? "" : leadText) : builder.toString(); default: throw new XMLStreamException("Unexpected event type " + currentEventType, delegate.getLocation()); } } } 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 testGetElementText() throws XMLStreamException { XMLEventReader reader = createXmlEventReader("<alice david=\"edgar\">bob</alice>"); reader.next(); Assert.assertEquals("bob", reader.getElementText()); reader = createXmlEventReader("<alice david=\"edgar\">bob</alice>"); reader.next(); reader.peek(); Assert.assertEquals("bob", reader.getElementText()); }