JAXB Java Object 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 (Java Architecture for XML Binding) API allows us to convert Java Object to XML file or simple XML string.
JAXB provides two main features – that are marshalling and unmarshalling. Marshalling allows us to convert Java objects to XML whereas unmarshalling used to convert XML to Object.
Here we will see how to marshal an object.
Add 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>
Creating a Java class, it’s going to be converted to an XML.
Book.java
public class Book {
    private int id;
    private String name;
    private double price;

    // constructor
    // getters and setters
    // tostring
}
Java Object to XML conversion using JAXB marshaller. Here we are simply converting the object to an XML string.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

public class ObjectToXML {

    public static void main(String[] args) {
        Book book = new Book(1,"Java",200);
        createXML(book);
    }

    private static void createXML(Book book) {
        try
        {
            JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            StringWriter sw = new StringWriter();
            marshaller.marshal(book, sw);
            System.out.println(sw.toString());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Book>
    <id>1</id>
    <name>Java</name>
    <price>200.0</price>
</Book>
We can also save the converted XML into a physical file using the marshal(Object obj, File location) overloaded method.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;

public class ObjectToXML {

    public static void main(String[] args) {
        Book book = new Book(1,"Java",200);
        createXMLFile(book);
    }

    private static void createXMLFile(Book book) {
        try
        {
            File file = new File("book.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(book, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
as an output, you can see the generated book.xml in your specified directory.
Java13 Text Blocks
JAXB XML to Java Obeject
JAXB Architecture
Happy Learning 🙂

JAXB XML to Java Object Conversion Example
JAXB Map to XML Conversion Example
Python String to int Conversion Example
Java String to int conversion Example
4 ways to create an Object in Java
Java – How to read CSV file and Map to Java Object
How to convert Java Object to JSON
How to convert JSON to Java Object Example
How to convert JSON to Java Map Object
Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Decimal To Binary Conversion Java Program
Decimal To Hex Conversion Java Program
Decimal To Octal Conversion Java Program
Octal To Decimal Conversion Java Program

JAXB XML to Java Object Conversion Example
JAXB Map to XML Conversion Example
Python String to int Conversion Example
Java String to int conversion Example
4 ways to create an Object in Java
Java – How to read CSV file and Map to Java Object
How to convert Java Object to JSON
How to convert JSON to Java Object Example
How to convert JSON to Java Map Object
Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Decimal To Binary Conversion Java Program
Decimal To Hex 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