/** * makes a {@link Document} out of a {@link XMLStreamReader} * * @param xmlStreamReader the xmlStreamRader to convert * @return the xmlStreamRader as {@link Document} * @throws FactoryConfigurationError * @throws XMLStreamException * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static Document getAsDocument(XMLStreamReader xmlStreamReader) throws XMLStreamException, FactoryConfigurationError, ParserConfigurationException, SAXException, IOException { StreamBufferStore store = new StreamBufferStore(); XMLStreamWriter xmlWriter = null; try { xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(store); xmlWriter.writeStartDocument(); XMLAdapter.writeElement(xmlWriter, xmlStreamReader); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (XMLStreamException e) { LOG.error("Unable to close xmlwriter."); } } } store.flush(); DOMParser parser = new DOMParser(); parser.parse(new InputSource(store.getInputStream())); Document doc = parser.getDocument(); store.close(); return doc; }
/** * Serializes the XML element (including all attributes and subnodes) from the given {@link * XMLStreamReader} into a {@link StreamBufferStore}. * * @param reader cursor must point at a <code>START_ELEMENT</code> event and points at the * corresponding <code>END_ELEMENT</code> event afterwards * @return stored document, never <code>null</code> * @throws IOException * @throws FactoryConfigurationError * @throws XMLStreamException */ public static StreamBufferStore serialize(XMLStreamReader reader) throws IOException, XMLStreamException, FactoryConfigurationError { StreamBufferStore tmpStore = new StreamBufferStore(); XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(tmpStore); try { copy(writer, reader); } finally { writer.close(); tmpStore.close(); } return tmpStore; }