Is there any way (with Java code examples, if possible) to convert, on the fly, an XML
input to JSON
, without any knowledge of the actual contents and the structure of the XML source (file, string, etc.)?
Say, for instance, that one has a very large XML dataset with unknown structure and multiple nesting levels, stored to a big text file. Reading everything into memory is not possible (for lack of space) and they want to convert this into JSON
directly, i.e., without having to write any code to detect and handle the StAX tags (e.g., START_ELEMENT
, CHARACTERS
, END_ELEMENT
).
The ideal solution would be to get a Reader
or InputStream
from the converter, so that, for instance, one supplies the XML file and the Reader
or InputStream
produces JSON
, to be fed to a FileOutputStream, or even directly to a JSON
parser like Jackson
. If that is not possible, at least a way of progressively reading an XML file, converting to JSON and writing to another file would be an acceptable compromise.
Tools that can be used for converting from/to XML/JSON (e.g., StaxON, JSON-lib, Jettison, XStream) do not seem to do that but only conversion of a known structure.
EDIT: Getting a Reader
or InputStream
from an OutputStream or a Writer (which would also cover the "conversion" I spoke of above), can be done in a number of ways, although for best results and "infinite" input sizes multithreading is involved. Solutions are described in this article in Ostermiller.org and a similar implementation can be found in the IO-Tools library.
Most of the applications use this format for transmitting the data from the server to the web page, or vice-versa. However, we can use the org. json. XML class in Java to convert the XML to JSON.
To convert an XML document to JSON, follow these steps: Select the XML to JSON action from the Tools > JSON Tools menu. Choose or enter the Input URL of the XML document. Choose the path of the Output file that will contain the resulting JSON document.
Yes, JSON is rapidly replacing XML for RPC-style communication: not just AJAX from browser, but server to server.
Here's a trivial example using Java's built-in StAX implementation to parse XML and Jettison to produce JSON from it. The XMLEventWriter has a convenient add(XMLEventWriter)
method for bridging a reader to a writer, making this super-simple:
import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import java.io.StringReader;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws Exception {
String xml =
"<root><foo>foo string</foo><bar><x>1</x><y>5</y></bar></root>";
XMLEventReader reader = XMLInputFactory.newInstance()
.createXMLEventReader(new StringReader(xml));
XMLEventWriter writer = new MappedXMLOutputFactory(new HashMap())
.createXMLEventWriter(System.out);
writer.add(reader);
writer.close();
reader.close();
}
}
I've created a self-contained Maven project demonstrating this on Github.
You can do this with StAXON, see https://github.com/beckchr/staxon/wiki/Converting-XML-to-JSON for sample code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With