@Test public void testGetElementText() throws XMLStreamException { XMLEventReader reader = createXmlEventReader("<alice david=\"edgar\">bob</alice>", ACCEPT_ALL); reader.next(); Assert.assertEquals("bob", reader.getElementText()); reader = createXmlEventReader("<alice david=\"edgar\">bob</alice>", ACCEPT_ALL); reader.next(); reader.peek(); Assert.assertEquals("bob", reader.getElementText()); }
@Override public String getElementText() throws XMLStreamException { if (currentEventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Expected start element event"); } StringBuilder builder = null; String leadText = null; while (true) { XMLEvent event = nextEvent(); String data = null; switch (event.getEventType()) { case XMLStreamConstants.ENTITY_REFERENCE: data = ((EntityReference) event).getName(); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: data = event.asCharacters().getData(); 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, event.getLocation()); } if (data != null) { if (leadText == null) { leadText = data; } else { if (builder == null) { builder = new StringBuilder(leadText); } builder.append(data); } } } }
SimpleXMLFilteredEventReader extends EventReaderDelegate { @Override public String getElementText() throws XMLStreamException { if (currentEventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Expected start element event"); } StringBuilder builder = null; String leadText = null; while (true) { XMLEvent event = nextEvent(); String data = null; switch (event.getEventType()) { case XMLStreamConstants.ENTITY_REFERENCE: data = ((EntityReference) event).getName(); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: data = event.asCharacters().getData(); 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, event.getLocation()); } if (data != null) { if (leadText == null) { leadText = data; } else { if (builder == null) { builder = new StringBuilder(leadText); } builder.append(data); } } } } }
SimpleXMLFilteredEventReader extends EventReaderDelegate { @Override public String getElementText() throws XMLStreamException { if (currentEventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Expected start element event"); } StringBuilder builder = null; String leadText = null; while (true) { XMLEvent event = nextEvent(); String data = null; switch (event.getEventType()) { case XMLStreamConstants.ENTITY_REFERENCE: data = ((EntityReference) event).getName(); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: data = event.asCharacters().getData(); 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, event.getLocation()); } if (data != null) { if (leadText == null) { leadText = data; } else { if (builder == null) { builder = new StringBuilder(leadText); } builder.append(data); } } } } SimpleXMLFilteredEventReader(XMLEventReader reader, EventFilter filter); }
SimpleXMLFilteredEventReader extends EventReaderDelegate { @Override public String getElementText() throws XMLStreamException { if (currentEventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Expected start element event"); } StringBuilder builder = null; String leadText = null; while (true) { XMLEvent event = nextEvent(); String data = null; switch (event.getEventType()) { case XMLStreamConstants.ENTITY_REFERENCE: data = ((EntityReference) event).getName(); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: data = event.asCharacters().getData(); 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, event.getLocation()); } if (data != null) { if (leadText == null) { leadText = data; } else { if (builder == null) { builder = new StringBuilder(leadText); } builder.append(data); } } } } SimpleXMLFilteredEventReader(XMLEventReader reader, EventFilter filter); @Override void setParent(XMLEventReader reader); @Override boolean hasNext(); @Override XMLEvent peek(); @Override Object next(); @Override XMLEvent nextEvent(); @Override String getElementText(); @Override XMLEvent nextTag(); }
SimpleXMLFilteredEventReader extends EventReaderDelegate { @Override public String getElementText() throws XMLStreamException { if (currentEventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Expected start element event"); } StringBuilder builder = null; String leadText = null; while (true) { XMLEvent event = nextEvent(); String data = null; switch (event.getEventType()) { case XMLStreamConstants.ENTITY_REFERENCE: data = ((EntityReference) event).getName(); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: data = event.asCharacters().getData(); 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, event.getLocation()); } if (data != null) { if (leadText == null) { leadText = data; } else { if (builder == null) { builder = new StringBuilder(leadText); } builder.append(data); } } } } SimpleXMLFilteredEventReader(XMLEventReader reader, EventFilter filter); @Override void setParent(XMLEventReader reader); @Override boolean hasNext(); @Override XMLEvent peek(); @Override Object next(); @Override XMLEvent nextEvent(); @Override String getElementText(); @Override XMLEvent nextTag();  }