Converting xml to json using jackson

16,334

I was able to get the solution to this problem by using org.json API to convert source XML to JSONObject and then to JSON by Jackson API.

Code

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

...
...

try (InputStream inputStream = new FileInputStream(new File(
                "source.xml"))) {
    String xml = IOUtils.toString(inputStream);
    JSONObject jObject = XML.toJSONObject(xml);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    Object json = mapper.readValue(jObject.toString(), Object.class);
    String output = mapper.writeValueAsString(json);
    System.out.println(output);
}

...
...
Share:
16,334
azhar_salati
Author by

azhar_salati

I am having total 9 years of web application development experience in the following area - 2 Years of experience in Java, GWT. 3 Years of experience in Java, JSP, Servlets, Javascript. 4 Years of experience in Spring, ExtJs 4.1, ExtJs 5.0, Sencha Touch, D3 JS, Angular JS, HTML5, CSS.

Updated on June 04, 2022

Comments

  • azhar_salati
    azhar_salati almost 2 years

    I want to convert an xml to json.

    The format of of xml is as follows -

     <default> 
          <column>                      
            <title>Title 1</title>
        <id>id1</id>
        <value>val1</value>
      </column>
      <column>
        <title>Title 2</title>
        <id>id2</id>
        <value>val2</value>
      </column>
      <column>
        <title>Title 3</title>
        <id>id3</id>
        <value>val3</value>
      </column>
      </default>
    

    And after the conversion i am expecting following json -

    {
        "column": [
            {
                "title": "Title 1",
                "id": "id1",
                "value": "val1"
            },
            {
                "title": "Title 2",
                "id": "id2",
                "value": "val2"
            },
            {
                "title": "Title 3",
                "id": "id3",
                "value": "val3"
            }
        ]
    }
    

    But when i use jackson for this purpose it gives me following json -

    {
        "column": {
            "title": "Title 3",
            "id": "id3",
            "value": "val3"
        }
    }
    

    I have tried using jackson 1.9 and jackson 2.1, but it didnt gave me the expected output.

    Can some one please let me know that whether it is possible or i need to change my xml format ? Following is the code that i have written to acheive the above scenario -

        try {
                XmlMapper xmlMapper = new XmlMapper();
                Map entries = xmlMapper.readValue(new File("xmlPath"), Map.class);
    
                ObjectMapper jsonMapper = new ObjectMapper();
                String json = jsonMapper.writeValueAsString(entries);
                System.out.println(json);
    
            } catch (Exception e) {
                e.printStackTrace();
            }       
    

    Thanks

  • divine
    divine over 6 years
    link is broken .