JAVA - Parsing an XML file into Map using FasterXml.Jackson

Zachi Shtain picture Zachi Shtain · Jul 13, 2017 · Viewed 8.3k times · Source

I have the following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Site xmlns="bla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  id="0" affiliations="foo" status="OPERATIONAL" color="#DB2253" updateTime="2002-12-17T09:30:47.0Z" name="GFB_0035" subject="GF">
    <location>
        <vertices Lat="41.905889" Lon="12.452019"/>
        <vertices Lat="41.905973" Lon="12.453596"/>
        <vertices Lat="41.905206" Lon="12.453707"/>
        <vertices Lat="41.905058" Lon="12.452109"/>
    </location>
</Site>

I am using FasterXml.Jackson to parse the data into a Map<String, Object> object using the following code:

XmlMapper xmlMapper = new XmlMapper();
Map<String, Object> data = xmlMapper.readValue(new File(xmlFile), Map.class);

However, when I look at the contents of the "location" field I have only one vertex instead of four (the last one).

UPDATE: The map object which I get looks like the one bellow:

{id=0, affiliations=foo, status=OPERATIONAL, color=#DB2253, updateTime=2002-12-17T09:30:47.0Z, name=GFB_0035, subject=GF, location={vertices={Lat=41.905058, Lon=12.452109}}}

Is there a way to get all four entries using FasterXml.Jackson?

Answer

Darshit Chokshi picture Darshit Chokshi · Jul 13, 2017

According to Java Documentation -

A map cannot contain duplicate keys; each key can map to at most one value.

And thats why when you look at the contents of the "location" field you have only one vertex instead of four (the last one).

I think you should parse the XML in your own classes instead of storing it into Map

See below example -

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Stack {


    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

        String xmlFile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<Site xmlns=\"bla\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  id=\"0\" affiliations=\"foo\""
                + " status=\"OPERATIONAL\" color=\"#DB2253\" updateTime=\"2002-12-17T09:30:47.0Z\" name=\"GFB_0035\" "
                + "subject=\"GF\">"
                + "    <location>"
                + "        <vertices lat=\"41.905889\" lon=\"12.452019\"/>"
                + "        <vertices lat=\"41.905973\" lon=\"12.453596\"/>"
                + "        <vertices lat=\"41.905206\" lon=\"12.453707\"/>"
                + "        <vertices lat=\"41.905058\" lon=\"12.452109\"/>"
                + "    </location>"
                + "</Site>";

        XmlMapper mapper = new XmlMapper();
        SiteData site = mapper.readValue(xmlFile, SiteData.class);
        System.out.println(site);
    }

}

SiteData.java

import com.fasterxml.jackson.xml.annotate.JacksonXmlProperty;
import com.fasterxml.jackson.xml.annotate.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "Site")
public class SiteData {

    private String id;

    private String affiliations;

    private String status;

    private String color;

    private String updateTime;

    private String name;

    private String subject;

    @JacksonXmlProperty(localName = "location")
    private LocationData location;

    public String getId() {
        return id;
    }

    public void setIdValue(String id) {
        this.id = id;
    }

    public String getAffiliations() {
        return affiliations;
    }

    public void setAffiliations(String affiliations) {
        this.affiliations = affiliations;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public LocationData getLocation() {
        return location;
    }

    public void setLocation(LocationData location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Site [id=" + id + ", affiliations=" + affiliations
                + ", status=" + status + ", color=" + color + ", updateTime="
                + updateTime + ", name=" + name + ", subject=" + subject
                + ", locations=" + location + "]";
    }
}

LocationData.java

import java.util.List;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;

public class LocationData {

    @JacksonXmlElementWrapper(useWrapping=false)
    private List<VerticesData> vertices;

    public List<VerticesData> getVertices() {
        return vertices;
    }

    public void setVertices(List<VerticesData> vertices) {
        this.vertices = vertices;
    }

    @Override
    public String toString() {
        return "LocationData [vertices=" + vertices + "]";
    }

}

VerticesData.java

public class VerticesData {

    private String lat;

    private String lon;

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    @Override
    public String toString() {
        return "Vertices [lat=" + lat + ", lon=" + lon + "]";
    }

}

Output:

Site [id=0, affiliations=foo, status=OPERATIONAL, color=#DB2253, updateTime=2002-12-17T09:30:47.0Z, name=GFB_0035, subject=GF, locations=LocationData [vertices=[Vertices [lat=41.905889, lon=12.452019], Vertices [lat=41.905973, lon=12.453596], Vertices [lat=41.905206, lon=12.453707], Vertices [lat=41.905058, lon=12.452109]]]]