Search in sources :

Example 1 with JsonXMLOutputFactory

use of de.odysseus.staxon.json.JsonXMLOutputFactory in project hale by halestudio.

the class JsonXML method toJson.

public static void toJson(Reader xmlReader, Writer jsonWriter) throws XMLStreamException, FactoryConfigurationError, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
    /*
		 * If we want to insert JSON array boundaries for multiple elements, we
		 * need to set the <code>autoArray</code> property. If our XML source
		 * was decorated with <code>&lt;?xml-multiple?&gt;</code> processing
		 * instructions, we'd set the <code>multiplePI</code> property instead.
		 * With the <code>autoPrimitive</code> property set, element text gets
		 * automatically converted to JSON primitives (number, boolean, null).
		 */
    JsonXMLConfig config = new JsonXMLConfigBuilder().namespaceDeclarations(true).autoArray(true).autoPrimitive(true).prettyPrint(false).build();
    /*
		 * Create source (XML).
		 */
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader);
    Source source = new StAXSource(reader);
    /*
		 * Create result (JSON).
		 */
    // create stream factory manually due to class loading issues
    XMLStreamWriter writer = new JsonXMLOutputFactory(config, new JsonStreamFactoryImpl()).createXMLStreamWriter(jsonWriter);
    Result result = new StAXResult(writer);
    /*
		 * 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) JsonXMLOutputFactory(de.odysseus.staxon.json.JsonXMLOutputFactory) JsonStreamFactoryImpl(de.odysseus.staxon.json.stream.impl.JsonStreamFactoryImpl) StAXSource(javax.xml.transform.stax.StAXSource) 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 JsonXMLOutputFactory (de.odysseus.staxon.json.JsonXMLOutputFactory)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