'Convert XML to JSON ignoring attributes

I'm trying to convert an XML to JSON in JAVA removing the tag attributes from the XML.

I tried using org.json.XML but it did not meet my needs.

Is there a library for doing what I want to do?

Example input:

<?xml version="1.0"?>
<company g="j">
    <staff id="1001">
        <firstname hi="5">jim</firstname>
        <lastname>fox</lastname>
    </staff>
    <staff id="2001">
        <firstname a="7">jay</firstname>
        <details tmp="0">
            <lastname>box</lastname>
            <nickname >fong fong</nickname>
            <salary id="99">200000</salary>
        </details>
    </staff>
</company>

Desired output:

{
    "company": {
        "staff": [
            {
                "firstname": "jim"
                "lastname": "fox",
            },
            {
                "firstname": "jay",
                "details": {
                    "lastname": "box",
                    "nickname": "fong fong",
                    "salary":"200000",
            }
        ]
    }
}

I tried the following but it convert the xml using the attributes:

package my.transform.data.utils;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.XML;
import org.json.JSONObject;

public class JSONObjectConverter {

    public static void main(String[] args) throws Exception {

        String xml = FileUtils.readFileToString(new File("src/main/resources/staff.xml"));
        JSONObject aJson = XML.toJSONObject(xml);
        System.out.println(aJson.toString());

    }

}

any suggestions?



Solution 1:[1]

Try doing an XSLT transformation to get the XML into the desired form before conversion. (You could also consider using the XSLT 3.0 xml-to-json() function).

I think it's very likely that any general-purpose converter will do exactly what you want without pre- or post- processing.

Solution 2:[2]

Underscore-java library has static methods U.fromXmlWithoutAttributes(string) and U.toJson(object). Live example

import com.github.underscore.U;
import java.util.Map;

public class JsonConversion {
    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        String xmlString  = "<?xml version=\"1.0\"?>"
        + "<company g=\"j\">"
        + "    <staff id=\"1001\">"
        + "        <firstname hi=\"5\">jim</firstname>"
        + "        <lastname>fox</lastname>"
        + "    </staff>"
        + "    <staff id=\"2001\">"
        + "        <firstname a=\"7\">jay</firstname>"
        + "        <details tmp=\"0\">"
        + "            <lastname>box</lastname>"
        + "            <nickname >fong fong</nickname>"
        + "            <salary id=\"99\">200000</salary>"
        + "        </details>"
        + "    </staff>"
        + "</company>"; 

        Map<String, Object> map = (Map) U.fromXmlWithoutAttributes(xmlString);  
        System.out.println(U.toJson(map));  
    }
}    

Output:

{
  "company": {
    "staff": [
      {
        "firstname": "jim",
        "lastname": "fox"
      },
      {
        "firstname": "jay",
        "details": {
          "lastname": "box",
          "nickname": "fong fong",
          "salary": "200000"
        }
      }
    ]
  }
}    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Kay
Solution 2