/** * Subclasses can override to customize the writer. * * @param outputFactory the factory to be used to create an {@link XMLEventWriter}. * @param writer the {@link Writer} to be used by the {@link XMLEventWriter} for * writing to character streams. * @return an xml writer * * @throws XMLStreamException thrown if error occured creating {@link XMLEventWriter}. */ protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException { return outputFactory.createXMLEventWriter(writer); }
XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent dtd = eventFactory .createDTD("<!DOCTYPE Employee SYSTEM \"xmlValidate.dtd\">"); XMLInputFactory inFactory = XMLInputFactory.newInstance(); XMLOutputFactory outFactory = XMLOutputFactory.newInstance(); XMLEventReader reader = inFactory .createXMLEventReader(new StreamSource( "xmlValidate.xml")); reader = new DTDReplacer(reader, dtd); XMLEventWriter writer = outFactory.createXMLEventWriter(System.out); writer.add(reader); writer.flush(); // TODO error and proper stream handling
@Override protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result); return new StaxEventHandler(eventWriter); }
@Before public void createStreamReader() throws Exception { stringWriter = new StringWriter(); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(stringWriter); streamWriter = new XMLEventStreamWriter(eventWriter, XMLEventFactory.newInstance()); }
@Test public void marshalStaxResultXMLEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); Result result = StaxUtils.createStaxResult(eventWriter); marshaller.marshal(flight, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@Test public void marshalJaxp14StaxResultEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); StAXResult result = new StAXResult(eventWriter); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@Test public void marshalStaxResultEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); Result result = StaxUtils.createStaxResult(eventWriter); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@Test public void eventWriterSource() throws Exception { StringWriter stringWriter = new StringWriter(); XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter); Reader reader = new StringReader(XML); Source source = new StreamSource(reader); StaxResult result = new StaxResult(eventWriter); assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter()); assertNull("StreamWriter returned", result.getXMLStreamWriter()); transformer.transform(source, result); assertThat("Invalid result", stringWriter.toString(), isSimilarTo(XML)); }
@Test public void standard() throws Exception { String xml = "<foo><bar>baz</bar></foo>"; List<XMLEvent> events = readEvents(xml); ListBasedXMLEventReader reader = new ListBasedXMLEventReader(events); StringWriter resultWriter = new StringWriter(); XMLEventWriter writer = this.outputFactory.createXMLEventWriter(resultWriter); writer.add(reader); assertThat(resultWriter.toString(), isSimilarTo(xml)); }
/** * Constructs a <code>STAXEventWriter</code> that writes events to the * provided character stream. * * @param writer * The character stream to which events will be written. * * @throws XMLStreamException * If an error occurs constructing an event writer from the * character stream. */ public STAXEventWriter(Writer writer) throws XMLStreamException { consumer = outputFactory.createXMLEventWriter(writer); }
/** * Constructs a <code>STAXEventWriter</code> that writes events to the * provided stream. * * @param stream * The output stream to which events will be written. * * @throws XMLStreamException * If an error occurs constructing an event writer from the * stream. */ public STAXEventWriter(OutputStream stream) throws XMLStreamException { consumer = outputFactory.createXMLEventWriter(stream); }
/** * Constructs a <code>STAXEventWriter</code> that writes events to the * provided file. * * @param file * The file to which events will be written. * * @throws XMLStreamException * If an error occurs creating an event writer from the file. * @throws IOException * If an error occurs openin the file for writing. */ public STAXEventWriter(File file) throws XMLStreamException, IOException { consumer = outputFactory.createXMLEventWriter(new FileWriter(file)); }
private static XMLEventWriter createXmlFragmentWriter(Writer theWriter) throws FactoryConfigurationError, XMLStreamException { XMLOutputFactory outputFactory = getOrCreateFragmentOutputFactory(); return outputFactory.createXMLEventWriter(theWriter); }
public static XMLEventWriter createXmlWriter(Writer theWriter) throws FactoryConfigurationError, XMLStreamException { XMLOutputFactory outputFactory = getOrCreateOutputFactory(); return outputFactory.createXMLEventWriter(theWriter); }
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(baos);
XMLInputFactory inFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inFactory.createXMLEventReader(new FileInputStream("1.xml")); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLEventWriter writer = factory.createXMLEventWriter(new FileWriter(file)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); writer.add(event); if (event.getEventType() == XMLEvent.START_ELEMENT) { if (event.asStartElement().getName().toString().equalsIgnoreCase("book")) { writer.add(eventFactory.createStartElement("", null, "index")); writer.add(eventFactory.createEndElement("", null, "index")); } } } writer.close();
/** * Get the XML event writer. * @param result The output result. * @return The XML event writer. * @throws XMLStreamException For errors obtaining an XML event writer. */ public static XMLEventWriter getXMLEventWriter(final Result result) throws XMLStreamException { return XML_OUTPUT_FACTORY.createXMLEventWriter(result); }
private void writeThrowExceptions(File output) throws FactoryConfigurationError, XMLStreamException, IOException, FileNotFoundException { try (final Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output), UTF_8))) { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory .createXMLEventWriter(out); writeThrowExceptions(eventWriter); } }
private void createEventWriter() throws XMLStreamException { if (eventWriter == null) { WebServiceMessage response = messageContext.getResponse(); eventWriter = getEventWriter(response.getPayloadResult()); if (eventWriter == null) { // as a final resort, use a stream, and transform that at endDocument() os = new ByteArrayOutputStream(); eventWriter = getOutputFactory().createXMLEventWriter(os); } } } }
@Override public void transform(Object object) throws XMLStreamException { if (xmlEventWriter == null) { baos = new UnsyncByteArrayOutputStream(); xmlEventWriter = getXmlOutputFactory().createXMLEventWriter(baos); } xmlEventWriter.add((XMLSecEvent) object); }