java jaxb unmarshall xml to map

// An example of using JAXB to convert XML to MAP

private static void unMarshalingExample() throws JAXBException 
{
    JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeMap.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    EmployeeMap empMap = (EmployeeMap) jaxbUnmarshaller.unmarshal( new File("c:/temp/employees.xml") );
     
    for(Integer empId : empMap.getEmployeeMap().keySet())
    {
        System.out.println(empMap.getEmployeeMap().get(empId).getFirstName());
        System.out.println(empMap.getEmployeeMap().get(empId).getLastName());
    }
}
     
Output:
 
Lokesh
Gupta
John
Mclane

4.4
5
Reza_va 95 points

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

4.4 (5 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source