xml2json is a Java library allowing to transform any XML string to a JSON format. The datatype interpretation may be customized via a property file : thus, a known xpath may be forced to a Date type, respecting a given format, a numeric value, an array, a string, ... This project is based on the json.org library (https://github.com/douglascrockford/JSON-java)
This project is released under version 2.0 of the [Apache License][]
Include the jar and use the XmlToJsonService class to convert your flow. The properties file included in the "ConfigurationMapEnumTypes" constructor sets the mapping. It relies on the folowing format :
xpath=datatype
where :
- xpath is an xml xpath (dot as field separator)
- datatype takes its value in
. number
. date|yyyy-MM-dd (in this case, the date format is given before the pipe caracter)
. boolean
. array
Example :
bloc_date.blocid.attr_date=date|yyyy-MM-dd HH:mm:ss
bloc_boolean2.blocid2.attr_bool=boolean
bloc.test_liste.test_liste=array
package com.pagesjaunes.json;
import com.pagesjaunes.json.JSONException;
import com.pagesjaunes.json.config.ConfigurationMapEnumTypes;
import com.pagesjaunes.json.service.XmlToJsonService;
public class MyConvertClass {
private ConfigurationMapEnumTypes conf;
private XmlToJsonService stXmlToJson;
public MyConvertClass() {
this.conf = new ConfigurationMapEnumTypes("xml2json-datatype-mapping.properties");
this.stXmlToJson = new XmlToJsonService(conf.getProperties());
}
public String doMyConvertion() throws JSONException {
String xml = "<bloc>"
+ "<blocid>00413695C0001</blocid>"
+ "<test_liste>test liste 1</test_liste>"
+ "<test_liste>test liste 2</test_liste>"
+ "</bloc>";
return stXmlToJson.toJSONObject(xml).toString());
}
}