TigerXmlWriter extends JCasFileWriter_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { OutputStream docOS = null; XMLEventWriter xmlEventWriter = null; try { docOS = getOutputStream(aJCas, filenameSuffix); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); xmlEventWriter = new IndentingXMLEventWriter( xmlOutputFactory.createXMLEventWriter(docOS, targetEncoding)); JAXBContext context = JAXBContext.newInstance(TigerSentence.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); XMLEventFactory xmlef = XMLEventFactory.newInstance(); xmlEventWriter.add(xmlef.createStartDocument()); xmlEventWriter.add(xmlef.createStartElement("", "", "corpus")); xmlEventWriter.add(xmlef.createStartElement("", "", "body")); int sentenceNumber = 1; for (Sentence s : select(aJCas, Sentence.class)) { TigerSentence ts = convertSentence(s, sentenceNumber); marshaller.marshal(new JAXBElement<TigerSentence>(new QName("s"), TigerSentence.class, ts), xmlEventWriter); sentenceNumber++; } xmlEventWriter.add(xmlef.createEndElement("", "", "body")); xmlEventWriter.add(xmlef.createEndElement("", "", "corpus")); xmlEventWriter.add(xmlef.createEndDocument()); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } finally { if (xmlEventWriter != null) { try { xmlEventWriter.close(); } catch (XMLStreamException e) { getLogger().warn("Error closing the XML event writer", e); } } closeQuietly(docOS); } }  @Override void process(JCas aJCas); static final String PARAM_FILENAME_EXTENSION; static final String PARAM_TARGET_ENCODING; }
@Test public void test() throws Exception { File targetFolder = testContext.getTestOutputFolder(); AnalysisEngine parser = createEngine(OpenNlpParser.class, OpenNlpParser.PARAM_WRITE_POS, true); JCas jcas = TestRunner.runTest(parser, "en", "This is a test ."); DocumentMetaData meta = DocumentMetaData.create(jcas); meta.setCollectionId("nocollection"); meta.setDocumentId("dummy"); AnalysisEngine writer = createEngine(TigerXmlWriter.class, TigerXmlWriter.PARAM_TARGET_LOCATION, targetFolder); writer.process(jcas); try ( Reader expected = new InputStreamReader(new FileInputStream( "src/test/resources/simple-sentence.xml"), "UTF-8"); Reader actual = new InputStreamReader(new FileInputStream( new File(targetFolder, "dummy.xml")), "UTF-8"); ) { XMLAssert.assertXMLEqual(expected, actual); } }