Why you should prefer XML to JSON

If you are developing Single Page Applications with JavaScript, REST Services play a central role. REST Services allow you to deal with data in a simple way. In most JavaScript applications REST Services based on JSON become very important in particular. But the JSON format can cause problems if you need to process type save objects. In this case, XML is much more reliable.
See the following short example of a java class:

value List<?> value=new ListArray();
value.add("Imixs Workflow");
value.add(new Integer(mebersCount));
value.add(new Date());
return result;

Using the Java Enterprise Edition this object will be mapped by the JAXB specification into the following XML structure

<value xsi:type="xs:string">Imixs Workflow</value>
<value xsi:type="xs:int">174</value>
<value xsi:type="xs:dateTime">2015-08-03T11:40:31.011+02:00</value>

But the same object can be represented in JSON like this:

["value":"Imixs Workflow","value":174,"value":1438594831011]

This is a result generated by the RestEasy Implementation used from JBoss/Wildfly. The same object will look differnt like you can see in the following example wen processed by Jersey which is used in GlassFish

["value":{"@type":"xs:string","$":"Imixs Workflow"},"value":{"@type":"xs:int","$":"174"},"value":{"@type":"xs:dateTime","$":"2015-08-04T17:19:20.689+02:00"}]

You can see that in the second JSON example the type information is provided like in the XML before. The reason why there is so much difference in the output is because the Java-JSON mapping is not standardized like the Java-XML mapping in JAX-B. So depending on your JSON Implementation the result can be differ in much details concerning object types. At the first look is seems not to be a big problem, but the lack of type information in RestEasy can result in problems when processing such objects in JavaScript. Especially if you need to add new values or modify values during run-time.

Imixs-Workflow provides a REST Service API which allows you to switch easily between different output formats like HTML, JSON or XML. To be sure hat you did not lost information when consuming a REST Service from the Imixs-Workflow platform it is recommended to use the XML format.
But how can you deal with XML in your JavaScript application? The solution is to convert the XML result into a JSON object. This can be done very easily using the JavaScript DOM Parser. See the following example provided by By David Walsh (http://davidwalsh.name/convert-xml-json)


// Changes XML to JSON
function xmlToJson(xml) {
 // Create the return object
 var obj = {};

 if (xml.nodeType == 1) { // element
 // do attributes
 if (xml.attributes.length > 0) {
 obj["@attributes"] = {};
 for (var j = 0; j < xml.attributes.length; j++) {
 var attribute = xml.attributes.item(j);
 obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
 } else if (xml.nodeType == 3) { // text
 obj = xml.nodeValue;

 // do children
 if (xml.hasChildNodes()) {
 for(var i = 0; i < xml.childNodes.length; i++) {
 var item = xml.childNodes.item(i);
 var nodeName = item.nodeName;
 if (typeof(obj[nodeName]) == "undefined") {
 obj[nodeName] = xmlToJson(item);
 } else {
 if (typeof(obj[nodeName].push) == "undefined") {
 var old = obj[nodeName];
 obj[nodeName] = [];
 return obj;

This is an easy example how you can convert any XML result into a JSON format without loosing type information.
Imxis-Workflow provides you with a JavaScript implementation which allows you to convert a dataset provided by the Imixs REST API directly into a type-save JSON object. This makes it very easy to write stable JavaScript clients with Imixs-Workflow preserving the flexibility of the Imixs ItemCollection independent from your platform or service implementation.

One comment

  • October 9, 2015 - 3:32 am | Permalink

    Found another tool which does the same thing http://codebeautify.org/xmltojson.

  • Leave a Reply