🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will learn how to convert Java object to XML using Java Architecture for XML Binding (JAXB).
Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java objects are converted from and to XML. It uses a standard set of mappings.
JAXB API provides a Marshaller interface, we can marshal(write) method to convert Java object into an XML document.
Let's see the steps to convert Java object into an XML document.
- Create POJO or bind the schema and generate the classes
- Create the JAXBContext object
- Create the Marshaller objects
- Create the content tree by using set methods
- Call the marshal method
Create POJO classes and Add JAXB annotations
Some basic and useful JAXB annotations are:
- @XmlRootElement: This is a must-have an annotation for the Object to be used in JAXB. It defines the root element for the XML content.
- @XmlType: It maps the class to the XML schema type. We can use it for ordering the elements in the XML.
- @XmlTransient: This will make sure that the Object property is not written to the XML.
- @XmlAttribute: This will create the Object property as an attribute.
- @XmlElement(name = “ABC”): This will create the element with name “ABC”
Book POJO Class
package net.javaguides.javaxmlparser.jaxb;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "book")
@XmlType(propOrder = {
"author",
"name",
"publisher",
"isbn"
})
public class Book {
private String name;
private String author;
private String publisher;
private String isbn;
@XmlElement(name = "title")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
}
Bookstore POJO Class
package net.javaguides.javaxmlparser.jaxb;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(namespace = "net.javaguides.javaxmlparser.jaxb")
public class Bookstore {
@XmlElementWrapper(name = "bookList")
@XmlElement(name = "book")
private List < Book > bookList;
private String name;
private String location;
public void setBookList(List < Book > bookList) {
this.bookList = bookList;
}
public List < Book > getBooksList() {
return bookList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
Marshaller Class - Convert Java Object to an XML
package net.javaguides.javaxmlparser.jaxb;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
/**
* Marshaller Class - Convert Java Object to XML
*
* @author Ramesh Fadatare
*
*/
public class BookMain {
private static final String BOOKSTORE_XML = "bookstore-jaxb.xml";
public static void main(String[] args) throws JAXBException, IOException {
List < Book > bookList = new ArrayList < Book > ();
// create books
Book book1 = new Book();
book1.setIsbn("978-0134685991");
book1.setName("Effective Java");
book1.setAuthor("Joshua Bloch");
book1.setPublisher("Amazon");
bookList.add(book1);
Book book2 = new Book();
book2.setIsbn("978-0596009205");
book2.setName("Head First Java, 2nd Edition");
book2.setAuthor("Kathy Sierra");
book2.setPublisher("amazon");
bookList.add(book2);
// create bookstore, assigning book
Bookstore bookstore = new Bookstore();
bookstore.setName("Amazon Bookstore");
bookstore.setLocation("Newyorkt");
bookstore.setBookList(bookList);
convertObjectToXML(bookstore);
}
private static void convertObjectToXML(Bookstore bookstore) throws JAXBException, FileNotFoundException {
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Bookstore.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
m.marshal(bookstore, System.out);
// Write to File
m.marshal(bookstore, new File(BOOKSTORE_XML));
}
}
The above program creates a file named bookstore-jaxb.xml and stored Book objects into this XML file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookstore xmlns:ns2="net.javaguides.javaxmlparser.jaxb">
<bookList>
<book>
<author>Joshua Bloch</author>
<title>Effective Java</title>
<publisher>Amazon</publisher>
<isbn>978-0134685991</isbn>
</book>
<book>
<author>Kathy Sierra</author>
<title>Head First Java, 2nd Edition</title>
<publisher>amazon</publisher>
<isbn>978-0596009205</isbn>
</book>
</bookList>
<location>Newyorkt</location>
<name>Amazon Bookstore</name>
</ns2:bookstore>
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment