Computer Science Engineer having more than 16 years of experience, predominantly in the Enterprise Product Development Space. Professionally started with LabWindows, then to X-Windows/Motif and landed in using Java. Primitive experience in VC++ and .Net. Making hands dirty with DSLs in the free time (?) For more about me, visit http://jagannathan.info Jagannathan is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

XML->JSON->HashMap

02.18.2013
| 5689 views |
  • submit to reddit

Yes, it is long time since i posted…

Was just trying to see how a XML can be converted to JSON and to HashMap.

The situation is very imaginary.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    import net.sf.json.JSON;
    import net.sf.json.xml.XMLSerializer;

    import org.apache.commons.io.IOUtils;
    import org.codehaus.jackson.JsonGenerationException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.type.TypeReference;

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

    InputStream is = new FileInputStream(new File(
    “e:\\jagannathan\\personal\\java-projects\\secondtest.xml”));
    String xml = IOUtils.toString(is);

    XMLSerializer xmlSerializer = new XMLSerializer();
    JSON json = xmlSerializer.read(xml);

    System.out.println(json.toString(2));

    printJSON(json.toString(2));

    }

    public static void printJSON(String jsonString) {
    ObjectMapper mapper = new ObjectMapper();

    try {

    Map<String, Object> jsonInMap = mapper.readValue(jsonString,
    new TypeReference<Map<String, Object>>() {
    });

    List<String> keys = new ArrayList<String>(jsonInMap.keySet());

    for (String key : keys) {
    System.out.println(key + “: ” + jsonInMap.get(key));
    }

    } catch (JsonGenerationException e) {
    e.printStackTrace();
    } catch (JsonMappingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

Dependencies

    <dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
    </dependency>
    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.3</version>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>xom</groupId>
    <artifactId>xom</artifactId>
    <version>1.2.5</version>
    </dependency>

    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.0</version>
    </dependency>

The Input XML

<?xml version=”1.0″ encoding=”UTF-8″?>
<company>
<name>Jags Inc</name>
<employees>
<employee>
<name>Jagan</name>
<sex>Male</sex>
<dob>24-jul</dob>
</employee>
<employee>
<name>Satya</name>
<sex>Male</sex>
<dob>24-apr</dob>
</employee>
</employees>
</company>

The output

7 Feb, 2013 7:20:50 PM net.sf.json.xml.XMLSerializer getType
INFO: Using default type string
{
“name”: “Jags Inc”,
“employees”: [
{
"name": "Jagan",
"sex": "Male",
"dob": "24-jul"
},
{
"name": "Satya",
"sex": "Male",
"dob": "24-apr"
}
]
}
name: Jags Inc
employees: [{name=Jagan, sex=Male, dob=24-jul}, {name=Satya, sex=Male, dob=24-apr}]

 

Published at DZone with permission of Jagannathan Asokan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)