XmlRecordReader implements RecordReader<String> { @Override public XmlRecord readRecord() throws Exception { if (hasNextRecord()) { StringBuilder stringBuilder = new StringBuilder(); while (!nextTagIsRootElementEnd()) { XMLEvent xmlEvent = xmlEventReader.nextEvent(); if (xmlEvent.isStartElement()) { escapeStartElementAttributes(stringBuilder, xmlEvent); } else if (xmlEvent.isEndElement()) { writeEndElement(stringBuilder, xmlEvent); } else { if (xmlEvent.isCharacters()) { stringBuilder.append(escape(xmlEvent.asCharacters().getData())); } } } writeEndElement(stringBuilder, xmlEventReader.nextEvent()); Header header = new Header(++currentRecordNumber, getDataSourceName(), LocalDateTime.now()); return new XmlRecord(header, stringBuilder.toString()); } else { return null; } } XmlRecordReader(final InputStream xmlInputStream, final String rootElementName);  XmlRecordReader(final InputStream xmlInputStream, final String rootElementName, final Charset charset); @Override void open(); @Override XmlRecord readRecord(); @Override void close();  }
@Test public void testReadRecord() throws Exception { InputStream dataSource = Mockito.spy(getDataSource("/persons.xml")); xmlRecordReader = new XmlRecordReader(dataSource, "person"); xmlRecordReader.open(); String expectedPayload = getXmlFromFile("/person.xml"); XmlRecord xmlRecord = xmlRecordReader.readRecord(); String actualPayload = xmlRecord.getPayload(); XMLUnit.setIgnoreWhitespace(true); Diff diff = new Diff(expectedPayload, actualPayload); assertThat(xmlRecord.getHeader().getNumber()).isEqualTo(1); assertThat(diff.similar()).isTrue(); xmlRecordReader.close(); verify(dataSource).close(); }