2

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

azhar_salati
  • 1,554
  • 5
  • 28
  • 54
  • It looks like just the last element is being converted. You should add an [SSCCE](http://sscce.org/) to your question, illustrating how you are performing this conversion. – Perception Feb 25 '13 at 15:36
  • What _exactly_ are you doing? Your description only talks about expected input, output, but not what you are doing to (try to) get that. – StaxMan Feb 25 '13 at 19:23

3 Answers3

2

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

...
...
Aman
  • 1,170
  • 3
  • 15
  • 29
0

While working on a group project I ran into the same issue you have described. The solution we used to get this to work was to change our Maven dependency from Jackson to json-lib. We used this site as a guide: http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java/

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <type>jar</type>
    <classifier>jdk15</classifier>
    <scope>compile</scope>
</dependency>

--$pli7

whatthemess
  • 21
  • 1
  • 6
0

I also ran into this issue, using Jackson 2.5. My solution was to replace the implementation of javax.json.JsonObjectBuilder in use with my own, which when a new object is added behaves like this :

public class CustomJsonObjectBuilder implements JsonObjectBuilder {

  private final Map<String, JsonValue> jsonFragments;

  // other methods omitted for brevity

   public JsonObjectBuilder add(String name, JsonValue value) {
     if(jsonFragments.containsKey(name)) {
        JsonValue oldValue = jsonFragments.get(name);
        JsonArrayBuilder newBuilder = new JsonArrayBuilderImpl();
        if(oldValue instanceof JsonArray) {
            JsonArray theArray = (JsonArray) oldValue;
            for (JsonObject oldValues : theArray.getValuesAs(JsonObject.class)) {
                newBuilder.add(oldValues);
            }
        } else {
            newBuilder.add(oldValue);
        }
        newBuilder.add(value);
        jsonFragments.put(name, newBuilder.build());
     } else {
        jsonFragments.put(name, value);
     }
     return this;
   }
francesco foresti
  • 2,004
  • 20
  • 24