Wednesday, July 16, 2014



Creating XML file from the General JSON




I have written a Java class with following methods to create XML from the JSON generated by the UI. Here I have used the GSON library, added it as a dependency to my project .


  • parse(s)
  • artifactDetailstoXML(artifactDetails)
  • UItoXML(ui)
  • contentTtoXML(content)
  • relationshiptoXML(relationship)
  • writeToFile(name.getAsString(),XML)


parse(s) method

Is used to create JSON model from JSON.


JsonParser jsonParser = new JsonParser();
JsonElement rxtJsonElement = jsonParser.parse(jsonContentAsString);
JsonObject rxtJsonArtifactTypeObject = rxtJsonElement.getAsJsonObject().getAsJsonObject();


Then passed the JSON file as String content(jsonContentAsString) to parse method.
JsonObject is returned from parse method and that object is used to retrieve the hierarchical information that is needed to create the xml file.


artifactDetailstoXML(artifactDetails) method


Assumption was made that all mandatory fields are filled and only checked if user has input data to namespace, namespaceAttribute and lifecycle fields.

UitoXML(ui) method


Checked if the there are more that one column data exists and if it does JsonArray was created accordingly.

Separate JsonObjects are created for artifactDetails, ui, content and relationship and passed to different methods to create XML content accordingly. Advantage in creating separate XML sections is that it gives flexibility to make changes easily in separate methods.

Then the xml contents returned from each of the methods are added together and created the whole XML content and passed to writeToFile() method to be written to a .rxt file.

JsonObjects, JsonElements, JsonArrays were used to create XML.


JSON generation from UI is halfway done. But I am in difficulty in generating JSON for dynamic UI part and now working on it.


No comments:

Post a Comment