Friday 12 February 2016

XML to Json

Converting XML to JSON without knowing the xsd schema is a really useful trick for storing data into mongo db.  I'm commenting this because although it is easy I don't want to forget!

Maven Dependency

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20151123</version>
    </dependency>

Conversion

    String data; // Xml as a string

    final JSONObject jsonObject = XML.toJSONObject(data);
    doc.put("data", BsonDocument.parse(jsonObject.toString()));
    doc.put("rawData", data);









No comments:

Post a Comment