Welcome to our comprehensive guide on Java JAXP! In this lesson, we'll explore how to process XML data using Java. If you're new to XML, don't worry! We'll start from the basics and gradually move to more advanced topics. Let's dive in!
XML (eXtensible Markup Language) is a markup language used to store and transport data. It's widely used for data interchange between different systems due to its simplicity and flexibility.
Java Architecture for XML Binding (JAXB) is a Java API for XML processing. It provides a way to bind Java objects to XML documents, making it easier to read, write, and manipulate XML data.
To use JAXB, you need to have Java Development Kit (JDK) installed on your system. JAXB is included in the standard JDK distribution.
Before we start processing XML data, we need an XML schema (.xsd) file that describes the structure of our XML data.
Let's create a simple schema for a Book:
<!-- book.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="year" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>To generate Java classes from our XML schema, we'll use the xjc command-line tool that comes with JDK.
$ xjc -d out book.xsdThis command generates a Java package named out with classes corresponding to our XML schema.
Now, let's write some code to read and write XML data using JAXB.
import out.generated.package_name.Books;
import out.generated.package_name.Book;
// ...
Books books = new Books();
JAXBContext context = JAXBContext.newInstance(Books.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("books.xml");
Books unmarshalledBooks = (Books) unmarshaller.unmarshal(file);
for (Book book : unmarshalledBooks.getBook()) {
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("Year: " + book.getYear());
}import out.generated.package_name.Books;
import out.generated.package_name.Book;
import javax.xml.bind.JAXBEncoder;
import javax.xml.bind.Marshaller;
// ...
Books books = new Books();
Book book1 = new Book();
book1.setTitle("Clean Code");
book1.setAuthor("Robert C. Martin");
book1.setYear(2008);
books.getBook().add(book1);
Book book2 = new Book();
book2.setTitle("Design Patterns: Elements of Reusable Object-Oriented Software");
book2.setAuthor("Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides");
book2.setYear(1994);
books.getBook().add(book2);
JAXBContext context = JAXBContext.newInstance(Books.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File file = new File("output.xml");
marshaller.marshal(books, file);š” Pro Tip: Always use JAXBContext.newInstance(Class.class) to create a JAXB context, passing the classes you're working with.
In this tutorial, we've covered the basics of using JAXB for XML processing in Java. To take your skills to the next level, check out the following advanced topics:
What is JAXB used for in Java?
How can we generate Java classes from an XML schema using JDK?