java – How to convert XML data with attributes to JSON format

I am using this method to convert XML data to JSON.

public JSONObject convertXMLToJson(String xml) {
   JSONObject json = XML.toJSONObject(xml);
   System.out.println(json.toString(2));
   return json;
}

This is really effective for most XML data. But, it slightly fails when it comes to XML data with attributes.

For this sample input – John
I am getting it almost right except, @type is displayed as type.

What should I change in my code to get it in the proper format ?

Read more here: Source link