1

Trying to convert any type of XML file to JSON object structure . Different xml files are having varied depths of elements and sub-elements. creation of arrays when elements with same name are at same height I need a recursive function which create exact JSON Object for any structured XML file

3
  • Did you tried???? use XML.toJSONObject() method of org.json. Commented Oct 30, 2015 at 14:10
  • Yes it works but i need recursively Commented Oct 30, 2015 at 14:15
  • You can call this method recursively. Just try and if there is any problem then post Question with the code next time. Commented Oct 30, 2015 at 14:22

2 Answers 2

3
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nikunj.m
 */
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class xmlTojsonDom1 {

public static void main(String[] args) {
    try {
        File file = new File("D:/Noname1.xml");
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        if (doc.hasChildNodes()) {
            JSONArray ddd = printNote_1(doc.getChildNodes());
            System.out.println("ddd ::::: " + ddd);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

private static JSONArray printNote_1(NodeList nodeList) {
    JSONArray dataArr = new JSONArray();
    JSONObject dataObject = new JSONObject();
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            if (tempNode.hasChildNodes() && tempNode.getChildNodes().getLength() > 1) {
                JSONArray temArr = printNote_1(tempNode.getChildNodes());
                if (dataObject.containsKey(tempNode.getNodeName())) {
                    dataObject.getJSONArray(tempNode.getNodeName()).add(temArr.getJSONObject(0));
                } else {
                    dataObject.put(tempNode.getNodeName(), temArr);
                }
            } else {
                dataObject.put(tempNode.getNodeName(), tempNode.getTextContent());
            }
        }
    }
    dataArr.add(dataObject);
    return dataArr;
}

}

Sign up to request clarification or add additional context in comments.

Comments

0

Please refer to Jackson Mapper

 XmlMapper xmlMapper = new XmlMapper();

 String xml= FileUtils.readFileToString(new File("test_4.xsd.xml"),Charset.defaultCharset());

 JsonNode node = xmlMapper.readTree(xml.getBytes());

 ObjectMapper jsonMapper = new ObjectMapper();

 String json = jsonMapper.writeValueAsString(node);

 System.out.println(json);

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.