Java XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks



shape Description

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:

shape 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*; 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 =; [/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] [java] package com.splessons.xml; import; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import; public class DOM4JParserDemo { public static void main(String[] args) { try { File inputFile = new File("input.txt"); // - Build the DOM4J document from the xml source SAXReader reader = new SAXReader(); Document document = 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]


shape Key Points

  • DOM4J parser is its working is fast and flexible.