I need to convert XML to JSON, and I have the following code that works fine. The problem, however, arises when the XML element must actually be converted to an array. My question has two parts:
1) What is the correct way to represent an array in xml?
Here is the xml I'm using now. The content of the elements must be an array. Therefore, the elements [0] must be an element inside.
<project id="200">
<name>test</name>
<elements>
<element>
<id>body</id>
<width>200</width>
<height>400</height>
<children/>
</element>
</elements>
</project>
2) How to convert xml to JSON containing JSON arrays as well as objects?
private String xmlToJson(String xml) throws IOException {
JSONObject jsonObject = XML.toJSONObject(xml);
return jsonObject.toString(4);
}
Many thanks
source
share