Java P
Java P

Reputation: 2281

Parsing XML and populating into Map

I have xml as below.

<Employees>
      <Employee id="1">Chuck</Employee>
      <Employee id="2">Al</Employee>
      <Employee id="3">Kiran</Employee>
</Employees>

XML contains huge number of employees.I have mentioned only for simplification.

What is the best way to parse this xml and populate into a map? Map should contain id and name pairs.

Please provide code for better understanding.

Upvotes: 0

Views: 2678

Answers (3)

bdoughan
bdoughan

Reputation: 148987

The XStream answer seems like a lot of code. You could do something like the following with the StAX APIs in the JDK/JRE:

package forum11871952;

import java.io.FileReader;
import java.util.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/forum11871952/input.xml"));
        xsr.nextTag(); // advance to Employees tag
        xsr.nextTag(); // advance to first Employer element
        Map<String,String> map = new HashMap<String,String>();
        while(xsr.getLocalName().equals("Employee")) {
            map.put(xsr.getAttributeValue("", "id"), xsr.getElementText());
            xsr.nextTag(); // advance to next Employer element
        }
    }

}

Upvotes: 4

user1318738
user1318738

Reputation:

We can simply map this using Xstream.

XStream xStream = new XStream(new DomDriver());
xStream.alias("Employees", Employees.class);
xStream.registerConverter(new MapEntryConverter());
employeesMap = (Map<String, String>) xStream.fromXML(queryXML);

Create a converter which unmarshall XML to Map object

private static class MapEntryConverter implements Converter {
        public boolean canConvert(Class clazz) {
            return clazz.equals(Employees.class);
        }

        public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
            AbstractMap<String, String> map = (AbstractMap<String, String>) value;
            for (Map.Entry<String, String> entry : map.entrySet()) {
                writer.startNode(entry.getKey().toString());
                writer.setValue(entry.getValue().toString());
                writer.endNode();
            }
        }

        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            Map<String, String> map = new HashMap<String, String>();

            while (reader.hasMoreChildren()) {
                reader.moveDown();
                map.put(reader.getAttribute(1), reader.getValue());
                reader.moveUp();
            }
            return map;
        }
    }

Create Employees and Employee classes as below.

private class Employees{
        List<Employee> employees;
    }
    private class Employee{
        private String id;
        private String value;
}

Hope this works for you.

Upvotes: 1

adarshr
adarshr

Reputation: 62583

Use a library such as XStream. List<Employee> suits better than a Map here.

Upvotes: 1

Related Questions