Search in sources :

Example 1 with PrettyXMLStreamWriter

use of de.odysseus.staxon.xml.util.PrettyXMLStreamWriter in project hale by halestudio.

the class JsonXML method toXML.

public static void toXML(Reader jsonReader, Writer xmlWriter) throws XMLStreamException, FactoryConfigurationError, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
    /*
		 * If the <code>multiplePI</code> property is set to <code>true</code>,
		 * the StAXON reader will generate <code>&lt;xml-multiple&gt;</code>
		 * processing instructions which would be copied to the XML output.
		 * These can be used by StAXON when converting back to JSON to trigger
		 * array starts. Set to <code>false</code> if you don't need to go back
		 * to JSON.
		 */
    JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).build();
    /*
		 * Create source (JSON).
		 */
    XMLStreamReader reader = new JsonXMLInputFactory(config, new JsonStreamFactoryImpl()).createXMLStreamReader(jsonReader);
    Source source = new StAXSource(reader);
    /*
		 * Create result (XML).
		 */
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(xmlWriter);
    // format
    Result result = new StAXResult(new PrettyXMLStreamWriter(writer));
    // output
    /*
		 * Copy source to result via "identity transform".
		 */
    TransformerFactory.newInstance().newTransformer().transform(source, result);
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) JsonXMLConfigBuilder(de.odysseus.staxon.json.JsonXMLConfigBuilder) JsonXMLConfig(de.odysseus.staxon.json.JsonXMLConfig) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyXMLStreamWriter(de.odysseus.staxon.xml.util.PrettyXMLStreamWriter) JsonXMLInputFactory(de.odysseus.staxon.json.JsonXMLInputFactory) JsonStreamFactoryImpl(de.odysseus.staxon.json.stream.impl.JsonStreamFactoryImpl) StAXSource(javax.xml.transform.stax.StAXSource) PrettyXMLStreamWriter(de.odysseus.staxon.xml.util.PrettyXMLStreamWriter) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) Result(javax.xml.transform.Result) StAXResult(javax.xml.transform.stax.StAXResult)

Aggregations

JsonXMLConfig (de.odysseus.staxon.json.JsonXMLConfig)1 JsonXMLConfigBuilder (de.odysseus.staxon.json.JsonXMLConfigBuilder)1 JsonXMLInputFactory (de.odysseus.staxon.json.JsonXMLInputFactory)1 JsonStreamFactoryImpl (de.odysseus.staxon.json.stream.impl.JsonStreamFactoryImpl)1 PrettyXMLStreamWriter (de.odysseus.staxon.xml.util.PrettyXMLStreamWriter)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 Result (javax.xml.transform.Result)1 Source (javax.xml.transform.Source)1 StAXResult (javax.xml.transform.stax.StAXResult)1 StAXSource (javax.xml.transform.stax.StAXSource)1