Parsing XML using DOM4J Java API

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 –

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

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.