List of usage examples for javax.xml.stream XMLOutputFactory createXMLEventWriter
public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream, String encoding) throws XMLStreamException;
From source file:eionet.webq.converter.JsonXMLBidirectionalConverter.java
/** * Template for conversion./* w ww. ja v a 2 s . co m*/ * * @param inputFactory input factory. * @param outputFactory output factory. * @param source source to convert. * @return conversion result as byte array. */ private byte[] convert(XMLInputFactory inputFactory, XMLOutputFactory outputFactory, byte[] source) { InputStream input = new ByteArrayInputStream(source); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { XMLEventReader reader = inputFactory.createXMLEventReader(input, "utf-8"); XMLEventWriter writer = outputFactory.createXMLEventWriter(output, "utf-8"); writer = new PrettyXMLEventWriter(writer); writer.add(reader); closeQuietly(reader, writer); return output.toByteArray(); } catch (XMLStreamException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } }
From source file:org.omnaest.utils.xml.XMLNestedMapConverter.java
/** * @param nestedMap/*from ww w . ja va 2s .com*/ * {@link Map} * @param outputStream * {@link OutputStream} * @param includeDocumentHeader * if true a xml document header is written out additionally */ private <K> void toXML(Map<K, Object> nestedMap, OutputStream outputStream, final ElementConverter<K, QName> keyElementConverter, boolean includeDocumentHeader) { // if (nestedMap != null && keyElementConverter != null && outputStream != null) { // try { // final XMLOutputFactory xmlOutputFactory = this.xmlInstanceContextFactory.newXmlOutputFactory(); final XMLEventFactory xmlEventFactory = this.xmlInstanceContextFactory.newXmlEventFactory(); Assert.isNotNull(xmlOutputFactory, "xmlOutputFactory must not be null"); Assert.isNotNull(xmlEventFactory, "xmlEventFactory must not be null"); // final XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(outputStream, this.encoding); final ExceptionHandler exceptionHandler = this.exceptionHandler; // try { // class Helper { /* ********************************************** Variables ********************************************** */ private List<String> namespaceStack = new ArrayList<String>(); /* ********************************************** Methods ********************************************** */ @SuppressWarnings("unchecked") public void write(Map<K, Object> map) { if (map != null) { for (K key : map.keySet()) { // final QName tagName = keyElementConverter.convert(key); final Object value = map.get(key); // if (value instanceof String) { // this.writeStartTag(tagName); // final String text = (String) value; this.writeText(text); // this.writeEndTag(tagName); } else if (value instanceof Map) { // this.writeStartTag(tagName); // final Map<K, Object> subMap = (Map<K, Object>) value; this.write(subMap); // this.writeEndTag(tagName); } else if (value instanceof List) { // final List<Object> valueList = (List<Object>) value; this.write(tagName, valueList); } } } } /** * @param tagName */ private void writeStartTag(QName tagName) { // try { // final String namespaceURI = tagName.getNamespaceURI(); // final Iterator<?> attributes = null; final Iterator<?> namespaces = StringUtils.isNotBlank(namespaceURI) && !StringUtils .equals(namespaceURI, ListUtils.lastElement(this.namespaceStack)) ? IteratorUtils.valueOf( xmlEventFactory.createNamespace(namespaceURI)) : null; StartElement startElement = xmlEventFactory.createStartElement(tagName, attributes, namespaces); xmlEventWriter.add(startElement); // this.namespaceStack.add(namespaceURI); } catch (Exception e) { exceptionHandler.handleException(e); } } /** * @param tagName */ private void writeEndTag(QName tagName) { // try { // final Iterator<?> namespaces = null; EndElement endElement = xmlEventFactory.createEndElement(tagName, namespaces); xmlEventWriter.add(endElement); // ListUtils.removeLast(this.namespaceStack); } catch (Exception e) { exceptionHandler.handleException(e); } } /** * @param text */ private void writeText(String text) { // try { // final Characters characters = xmlEventFactory.createCharacters(text); xmlEventWriter.add(characters); } catch (Exception e) { exceptionHandler.handleException(e); } } /** * @param tagName * @param valueList */ @SuppressWarnings("unchecked") private void write(QName tagName, List<Object> valueList) { if (valueList != null) { for (Object value : valueList) { // if (value != null) { // this.writeStartTag(tagName); // if (value instanceof Map) { // final Map<K, Object> map = (Map<K, Object>) value; this.write(map); } else if (value instanceof String) { // final String text = (String) value; this.writeText(text); } // this.writeEndTag(tagName); } } } } } // if (includeDocumentHeader) { xmlEventWriter.add(xmlEventFactory.createStartDocument()); } // new Helper().write(nestedMap); // xmlEventWriter.add(xmlEventFactory.createEndDocument()); } finally { xmlEventWriter.close(); outputStream.flush(); } } catch (Exception e) { if (this.exceptionHandler != null) { this.exceptionHandler.handleException(e); } } } }
From source file:org.xmlsh.commands.internal.xml2json.java
private byte[] serializeAsXML(XMLEventReader reader) throws XMLStreamException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLOutputFactory fact = new OutputFactory(); XMLEventWriter writer = fact.createXMLEventWriter(bos, kENCODING_UTF_8); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isEndElement() && event.asEndElement().getName().equals(kELEM_STRING)) break; writer.add(event);//from w w w .jav a 2 s . c o m } writer.flush(); writer.close(); return bos.toByteArray(); }