use of de.odysseus.staxon.json.JsonXMLConfig 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><?xml-multiple?></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);
}
use of de.odysseus.staxon.json.JsonXMLConfig 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><xml-multiple></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);
}
Aggregations