java jaxb unmarshall xml to map
Java
// 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>
// 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
Also in Java:
- Title
- convert two bytes to int java
- Category
- Java
- Title
- instantiate optinal java 8
- Category
- Java
- Title
- java list as parameter
- Category
- Java
- Title
- print int in java
- Category
- Java
- Title
- arraylist to array java
- Category
- Java
- Title
- charat(0).touppercase() java
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- sieve in java
- Category
- Java
- Title
- java question mark operator
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java replace all html tags
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- java read string input
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- how to count number of words in a string
- Category
- Java
- Title
- runtime java examples
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java program to get all cat indices in elasticsearch javadoc
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- processing audio android
- Category
- Java
- Title
- java convert a char[] to string
- Category
- Java
- Title
- print values of bst java
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- could not initialize defaultkotlinsourcesetkt intelij
- Category
- Java
- Title
- remove extra blank spaces from string in java
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- find maven version
- Category
- Java
- Title
- prime number program in java
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java loop through arraylist
- Category
- Java
- Title
- java public private key encryption
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- capacitor-android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- replace all punctuation in string java
- Category
- Java
- Title
- how to format a double in java to 2 decimal places
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- how to fill a 2d array in java
- Category
- Java
- Title
- get cursor position in textarea java
- Category
- Java
- Title
- socket programming in java
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- string equality in java
- Category
- Java
- Title
- how to check internet is working or not in java
- Category
- Java
- Title
- how to convert string to array in java
- Category
- Java
- Title
- regex get string between quotes java
- Category
- Java
- Title
- java split array into two
- Category
- Java
- Title
- java insert array
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- java how to change the length of an array
- Category
- Java
- Title
- how to use lambda in java
- Category
- Java
- Title
- java int to int array
- Category
- Java
- Title
- java create unmodifiable list
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- java making a screen
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- java jaxb unmarshall xml to map
- Category
- Java
- Title
- java interfaces
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- in java how to convert string to integer
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- logger in java
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- foreach java
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- java android edit text set value
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- how to add multiple filter condition in Java stream filter chain
- Category
- Java
- Title
- java read each lines in file
- Category
- Java
- Title
- how to print to the console in java
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- dice in java
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- check if a list contains a string java
- Category
- Java
- Title
- java check for string length
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- map multiple fields from java stream
- Category
- Java
- Title
- number of matches regex java
- Category
- Java