StAXSerializer implements XMLEventReader { public StAXSerializer(final XmlNodeReadOnlyTrx rtx) { this(rtx, true); } StAXSerializer(final XmlNodeReadOnlyTrx rtx);  StAXSerializer(final XmlNodeReadOnlyTrx pRtx, final boolean pCloseRtx); @Override void close(); @Override String getElementText(); @Override Object getProperty(final String name); @Override boolean hasNext(); @Override XMLEvent nextEvent(); @Override XMLEvent nextTag(); @Override XMLEvent peek(); @Override Object next(); @Override void remove();  }
@Test public void testStAXSerializer() { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final XmlSerializer xmlSerializer = new XmlSerializerBuilder(holder.getResourceManager(), out).emitXMLDeclaration().build(); xmlSerializer.call(); final XmlNodeReadOnlyTrx rtx = holder.getResourceManager().beginNodeReadOnlyTrx(); StAXSerializer serializer = new StAXSerializer(rtx); final StringBuilder strBuilder = new StringBuilder(); boolean isEmptyElement = false; while (serializer.hasNext()) { final XMLEvent event = serializer.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: strBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"); break; case XMLStreamConstants.START_ELEMENT: emitElement(event, strBuilder); if (serializer.peek().getEventType() == XMLStreamConstants.END_ELEMENT) { strBuilder.append("/>"); isEmptyElement = true; } else { strBuilder.append('>'); } break; case XMLStreamConstants.END_ELEMENT: if (isEmptyElement) { isEmptyElement = false; } else { emitQName(true, event, strBuilder); strBuilder.append('>'); } break; case XMLStreamConstants.CHARACTERS: strBuilder.append(((Characters) event).getData()); break; default: break; } } assertEquals(out.toString(), strBuilder.toString()); holder.getXmlNodeReadTrx().moveToDocumentRoot(); holder.getXmlNodeReadTrx().moveToFirstChild(); serializer = new StAXSerializer(holder.getXmlNodeReadTrx()); String elemText = null; if (serializer.hasNext()) { serializer.next(); elemText = serializer.getElementText(); } assertEquals("oops1foooops2baroops3", elemText); checkForException(serializer); if (serializer.hasNext()) { serializer.next(); elemText = serializer.getElementText(); } assertEquals("foo", elemText); checkForException(serializer); if (serializer.hasNext()) { serializer.next(); elemText = serializer.getElementText(); } assertEquals("", elemText); checkForException(serializer); checkForException(serializer); checkForException(serializer); if (serializer.hasNext()) { serializer.next(); elemText = serializer.getElementText(); } assertEquals("bar", elemText); if (serializer.hasNext()) { serializer.next(); elemText = serializer.getElementText(); } assertEquals("", elemText); checkForException(serializer); checkForException(serializer); checkForException(serializer); checkForException(serializer); checkForException(serializer); rtx.close(); } catch (final XMLStreamException e) { fail("XML error while parsing: " + e.getMessage()); } catch (final SirixException e) { fail("Sirix exception occured: " + e.getMessage()); } catch (final Exception e) { fail("Any exception occured: " + e.getMessage()); } }