Java converts XML to JSON and determines whether an array or object

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);

} // End of XML to JSON

Many thanks

+4
source share
1 answer

Answering your first question: What is the proper way to represent an array in xml?

, : http://www.w3.org/2005/07/xml-schema-patterns.html#Vector

0

Source: https://habr.com/ru/post/1528800/


All Articles