DOM4J is a library related to Java which helps to parse XML document. The advantages with DOM4J parser is its working is fast and flexible. Collections of Java such as Arrays and Lists are used. It can co-ordinate with other XML parsers like DOM, SAX, XPath and XSLT. It can parse huge XML document with very less memory footprint.
DOM4J parser must be when:
The developer should know everything about document structure.
To change the elements placed in the structure.
When the data in the document have to be used multiple times.
Example
Below is the example of XPATH parser which parses a XML document.
Step-1: Initially, the packages that are related to XML are imported.
[java]
import java.io.*;
import java.util.*;
import org.dom4j.*;
[/java]
Step-2: Then create the SAXBuilder class.
[java]
SAXBuilder saxBuilder = new SAXBuilder();
[/java]
Step-3: A XML document should be created from file or a stream.
[java]
File inputFile = new File("input.txt");
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(inputFile);
[/java]
Step-4: Now root element have to be extracted.
[java]
Element classElement = document.getRootElement();
[/java]
Step-5: The attributes and sub-elements are tested.
[java]
//returns specific attribute
valueOf("@attributeName");
[/java]
[java]
//returns first child node
selectSingleNode("subelementName");
[/java]
If kept all the above steps at once, below is the input XML document that has to be parsed.
[xml]
<?xml version="1.0"?>
<class>
<student rollno="393">
<firstname>John</firstname>
<lastname>Mike</lastname>
<nickname>Jom</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Rafeal</firstname>
<lastname>Nadal</lastname>
<nickname>Rafa</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Samuel</firstname>
<lastname>Johnson</lastname>
<nickname>Sam</nickname>
<marks>90</marks>
</student>
</class>
[/xml]
DOM4JParserDemo.java:
[java]
package com.splessons.xml;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class DOM4JParserDemo {
public static void main(String[] args) {
try {
File inputFile = new File("input.txt");
//SAXReader.read(xmlSource)() - Build the DOM4J document from the xml source
SAXReader reader = new SAXReader();
Document document = reader.read( inputFile );
//Document.getRootElement() - Get the root element of the XML
System.out.println("Root element :" + document.getRootElement().getName());
Element classElement = document.getRootElement();
List<Node> nodes = document.selectNodes("/class/student" );
System.out.println("----------------------------");
for (Node node : nodes) {
//Node.valueOf(@Name) - Get the value of an attribute with given name of the element
System.out.println("\nCurrent Element :" + node.getName());
System.out.println("Student roll no : " + node.valueOf("@rollno") );
System.out.println("First Name : " + node.selectSingleNode("firstname").getText());
System.out.println("Last Name : " + node.selectSingleNode("lastname").getText());
System.out.println("First Name : " + node.selectSingleNode("nickname").getText());
System.out.println("Marks : " + node.selectSingleNode("marks").getText());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
[/java]
Output:
[java]
Root element :class
----------------------------
Current Element :student
Student roll no : 393
First Name : John
Last Name : Mike
Nick Name : Jom
Marks : 85
Current Element :student
Student roll no : 493
First Name : Rafeal
Last Name : Nadal
Nick Name : Rafa
Marks : 95
Current Element :student
Student roll no : 593
First Name : Samuel
Last Name : Johnson
Nick Name : Sam
Marks : 90
[/java]