java jaxb unmarshall xml to map
// An Example of using JAXB to convert a map to XML , written in Java.
public static void main(String[] args) throws JAXBException
{
HashMap<Integer, Employee> map = new HashMap<Integer, Employee>();
Employee emp1 = new Employee();
emp1.setId(1);
emp1.setFirstName("Lokesh");
emp1.setLastName("Gupta");
emp1.setIncome(100.0);
Employee emp2 = new Employee();
emp2.setId(2);
emp2.setFirstName("John");
emp2.setLastName("Mclane");
emp2.setIncome(200.0);
map.put( 1 , emp1);
map.put( 2 , emp2);
//Add employees in map
EmployeeMap employeeMap = new EmployeeMap();
employeeMap.setEmployeeMap(map);
/******************** Marshalling example *****************************/
JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeMap.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(employeeMap, System.out);
jaxbMarshaller.marshal(employeeMap, new File("c:/temp/employees.xml"));
}
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees>
<employeeMap>
<entry>
<key>1</key>
<value>
<id>1</id>
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<income>100.0</income>
</value>
</entry>
<entry>
<key>2</key>
<value>
<id>2</id>
<firstName>John</firstName>
<lastName>Mclane</lastName>
<income>200.0</income>
</value>
</entry>
</employeeMap>
</employees>