0

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.

2
  • Use jdom and iterate over all employee entries adding into a Map<String, Integer>. Commented Aug 8, 2012 at 19:45
  • 1
    What have you tried? do you know the difference between SAX, DOM, XPATH, ect... please search stackoverflow before posting. I think this question has been answered about 1000 times. Commented Aug 8, 2012 at 20:02

3 Answers 3

4

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

3 Comments

List is better than a Map if OP doesn't care about lookup times.
how about Set over both List or Map
My requirement is to set in Map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.