JAXB Map to XML Conversion Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAXB is a very efficient framework to covert XML to Object and Object to XML in Java. In this tutorial, we are going to see how to convert Java Map to XML using JAXB.
Add the below jaxb-api dependency in pom.xml
<dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.3.1</version>
</dependency>
<!-- API, java.xml.bind module -->
       <dependency>
           <groupId>jakarta.xml.bind</groupId>
           <artifactId>jakarta.xml.bind-api</artifactId>
           <version>2.3.2</version>
       </dependency>

       <!-- Runtime, com.sun.xml.bind module -->
       <dependency>
           <groupId>org.glassfish.jaxb</groupId>
           <artifactId>jaxb-runtime</artifactId>
           <version>2.3.2</version>
       </dependency>
We can not convert the Java Map directly to XML because JAXB expects a @XmlRootElement annotation on top of the entity class to convert into XML. If you try to convert the Map object you would get the below exception.
The Exception clearly saying @XmlRootElement is mandatory for JAXBContext.
So to fix the issue we can create a Wrapper for the HashMap class by defining the @XmlRootElement annotation on top of the Wrapper class. Let’s see in practice.
In this example, I am going to create a map with a list of items, where a category is a key and the list of items are value.
So basically I need two wrapper classes to define my use-case: one is for List and another is for Map.
Creating ItemsList wrapper class to hold the items.
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemsList {
    private List<Item> item = new ArrayList<>();

    public List<Item> getItem() {
        return item;
    }
    
    public void setItem(List<Item> itemsList) {
        this.item = item;
    }
}

Create an ItemsMap class to hold the list of items by category.
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashMap;
import java.util.Map;

@XmlRootElement(name = "items")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemsMap {

    private Map<String, ItemsList> itemsMap = new HashMap<>();

    public Map<String, ItemsList> getItemsMap() {
        return itemsMap;
    }

    public void setItemsMap(Map<String, ItemsList> itemsMap) {
        this.itemsMap = itemsMap;
    }
}

Now the wrapper classes are ready to use. Now let’s prepare the list of items, make it as a map and convert the map into XML.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class MapToXML {
    public static void main(String[] args) throws JAXBException {
        // Creating HashMap to hold ItemsList (Wrapper for List)
        HashMap<String, ItemsList> map = new HashMap<>();
        map.put("electronics",getElecrtonics());
        map.put("books",getBooks());
    
        // Setting map to ItemsMap (Wrapper for HashMap)
        ItemsMap iMap = new ItemsMap();
        iMap.setItemsMap(map);

        JAXBContext jaxbContext = JAXBContext.newInstance(ItemsMap.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        // marshalling ItemsMap.
        jaxbMarshaller.marshal(iMap, System.out);
    }

    // Building the Electronic items
    public static ItemsList getElecrtonics(){
        ArrayList<Item> list = new ArrayList<Item>(Arrays.asList(new Item(100,"Samsung",15000.00),
                new Item(200,"iPhone10",110000.00)));
        ItemsList itemsList = new ItemsList();
        itemsList.getItem().addAll(list);
        return itemsList;
    }
    // Building the Book items
    public static ItemsList getBooks(){
        ArrayList<Item> list = new ArrayList<Item>(Arrays.asList(new Item(2000,"Core Java",500.00),
                new Item(3000,"Spring in Action",750.00)));
        ItemsList itemsList = new ItemsList();
        itemsList.getItem().addAll(list);
        return itemsList;
    }
}

Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
    <itemsMap>
        <entry>
            <key>electronics</key>
            <value>
                <item>
                    <itemId>100</itemId>
                    <itemName>Samsung</itemName>
                    <price>15000.0</price>
                </item>
                <item>
                    <itemId>200</itemId>
                    <itemName>iPhone10</itemName>
                    <price>110000.0</price>
                </item>
            </value>
        </entry>
        <entry>
            <key>books</key>
            <value>
                <item>
                    <itemId>2000</itemId>
                    <itemName>Core Java</itemName>
                    <price>500.0</price>
                </item>
                <item>
                    <itemId>3000</itemId>
                    <itemName>Spring in Action</itemName>
                    <price>750.0</price>
                </item>
            </value>
        </entry>
    </itemsMap>
</items>
Done!
JAXB Object to XML conversion
JAXB XML to Object Conversion
Happy Learning 🙂

JAXB XML to Java Object Conversion Example
JAXB Java Object to XML Conversion Example
How to Rotate Elements in List
Java 8 foreach Example Tutorials
Java Swing JList Example
Hibernate orderby criteria Example
Resolve NullPointerException in Collectors.toMap
Java String to int conversion Example
Decimal To Hex Conversion Java Program
Python String to int Conversion Example
Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Decimal To Binary Conversion Java Program
Decimal To Octal Conversion Java Program
Octal To Decimal Conversion Java Program

JAXB XML to Java Object Conversion Example
JAXB Java Object to XML Conversion Example
How to Rotate Elements in List
Java 8 foreach Example Tutorials
Java Swing JList Example
Hibernate orderby criteria Example
Resolve NullPointerException in Collectors.toMap
Java String to int conversion Example
Decimal To Hex Conversion Java Program
Python String to int Conversion Example
Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Decimal To Binary Conversion Java Program
Decimal To Octal Conversion Java Program
Octal To Decimal Conversion Java Program
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String