4

I need to convert XML into JSON and I have the following code which works fine. The problem, however, arises when an XML element should actually be converted into an array. My question is in two parts:

1) What is the proper way to represent an array in xml?

Here is the xml I'm currently using. The contents of elements should actually be an array. So elements[0] should be the element within.

<project id="200">
    <name>test</name>
    <elements>
        <element>
            <id>body</id>
            <width>200</width>
            <height>400</height>
            <children/>
        </element>
    </elements>
</project>

2) How can I convert the xml into 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

5
  • Please go through the previous answers on SO stackoverflow.com/a/1823328/1759128 Commented Feb 25, 2014 at 17:20
  • unfortunately that solution doesn't answer my question as it doesn't single out arrays Commented Feb 25, 2014 at 17:30
  • Your best bet is to use an XML tool to create the array as a List, then a JSON tool to convert the List to JSON. Commented Feb 25, 2014 at 17:31
  • @HotLicks, this qwuestion is - "how to do this" Commented Feb 25, 2014 at 17:33
  • @msangel - How to do what? There are multiple tools for both -- just lash two of them together. Commented Feb 25, 2014 at 17:35

2 Answers 2

1

There is an underscore-java library with static method U.xmlToJson(xml). It supports a special attribute array="true" which forces the element to be an array.

<project id="200">
    <name>test</name>
    <elements>
        <element array="true">
            <id>body</id>
            <width>200</width>
            <height>400</height>
            <children/>
        </element>
    </elements>
</project>

Output:

{
  "project": {
    "-id": "200",
    "name": "test",
    "elements": [
      {
        "id": "body",
        "width": "200",
        "height": "400",
        "children": {
          "-self-closing": "true"
        }
      }
    ]
  },
  "#omit-xml-declaration": "yes"
}    
Sign up to request clarification or add additional context in comments.

5 Comments

I know we have MODE to replace self-closing portion value as null, do we have any MODE to replace with EMPTY
I can add a new mode REPLACE_SELF_CLOSING_WITH_EMPTY.
It will be available in nearest release.
Is there a config/a way to discard "#omit-xml-declaration" key from response?
We may exclude keys with help of method U.remove(map, key). As alternative you may add xml header.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.