Practical XML Parsing Examples
In this section, we'll explore some practical examples of parsing XML files using the Java APIs discussed earlier.
Parsing a Simple XML File
Let's start with a simple XML file named books.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
</book>
<book>
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<year>1960</year>
</book>
</library>
Using the DOM parser, we can parse this file and extract the book information:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("books.xml");
NodeList bookNodes = document.getElementsByTagName("book");
for (int i = 0; i < bookNodes.getLength(); i++) {
Node bookNode = bookNodes.item(i);
System.out.println("Title: " + bookNode.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Author: " + bookNode.getElementsByTagName("author").item(0).getTextContent());
System.out.println("Year: " + bookNode.getElementsByTagName("year").item(0).getTextContent());
}
Parsing a Complex XML File
Now, let's consider a more complex XML file that includes namespaces:
<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://example.com/library"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<book>
<dc:title>The Catcher in the Rye</dc:title>
<dc:creator>J.D. Salinger</dc:creator>
<dc:date>1951</dc:date>
</book>
<book>
<dc:title>1984</dc:title>
<dc:creator>George Orwell</dc:creator>
<dc:date>1949</dc:date>
</book>
</library>
To parse this file and access the elements with namespaces, we can use the following code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("books.xml");
NodeList bookNodes = document.getElementsByTagNameNS("http://example.com/library", "book");
for (int i = 0; i < bookNodes.getLength(); i++) {
Node bookNode = bookNodes.item(i);
System.out.println("Title: " + bookNode.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "title").item(0).getTextContent());
System.out.println("Author: " + bookNode.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "creator").item(0).getTextContent());
System.out.println("Year: " + bookNode.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "date").item(0).getTextContent());
}
By understanding these practical examples, you should now have a solid grasp of how to parse XML files using Java APIs.