This article will help to write java program for xml using dom4j API. Dom4j is open source API for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP. Dom4j is easy to use and all the classes and methods are named reasonably.
The following articles would help you understand Java, XML and XML parsing better –
Features:
1. Event based processing mode to support for massive documents or XML streams
2. Based on Java interfaces for flexible plug and play implementations
3. Support for XML Schema Data Type
4. Designed for the Java platform with full support for the Java Collections Framework (Java 2 Collections)
5. Full support for JAXP, TrAX, SAX, DOM, and XSLT
6. Fully integrated XPath support for easy navigation of XML documents
Implementation:
Download the current release of dom4j API from download page.
Let’s begin with the CDCatalog.xml file, a simple well formatted XML file which shows CD catalog. It contains details about Title, Artist, Country, Company, Price and Year.
<? Xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire B</TITLE>
<ARTIST>John Dylan</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>XYZ</COMPANY>
<PRICE>12.75</PRICE>
<YEAR>1979</YEAR>
</CD>
<CD>
<TITLE>heart Beat</TITLE>
<ARTIST>Ronnie T</ARTIST>
<COUNTRY>USA</COUNTRY>
In dom4j, the Document object is initially created by using the read () method of SAXReader Object. It takes input as XML file path/ InputStream/
Reader/ URL and parses into a Document object.
getRootElement () of the Document object will return the root element, the root element elements () method will return list of CD element details. By using getName () and getData () method we can get the elements name and value.
Example XMLParser.java demonstrates a simple java program that parses the CDCatalog.xml file and displays the parsed XML output.
package com.infy.dom4j.test;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* Parser to read CD Catalog XML.
*
* @author Chaitanya Singh
* @version 1.0
*/
public class XMLParser {
public static void main(String[] args) {
String xmlPath = "CDCatalog.xml";
try {
SAXReader reader = new SAXReader();
Document document = reader.read(xmlPath);
Element rootElement = document.getRootElement();
System.out.println("Root Element: "+rootElement.getName());
Parsed output of the CDCatalog XML
Root Element: CATALOG
CD Catalog Size: 26
CD Catalog 1 Details
Element Name: TITLE
Element Value: Empire B
Element Name: ARTIST
Element Value: John Dylan
Element Name: COUNTRY
Element Value: UK
Element Name: COMPANY
Element Value: XYZ
Element Name: PRICE
Element Value: 12.75
Element Name: YEAR
Element Value: 1979
CD Catalog 2 Details
Element Name: TITLE
Element Value: heart Bea
Element Name: ARTIST
Element Value: Ronnie T
Element Name: COUNTRY
References:
http://www.dom4j.org/index.html
http://www.dom4j.org/download.html
http://www.dom4j.org/apidocs/index.html
Search keywords: dom4j, java parser, xml parser, java xml parser, dom4j api
Leave a Reply