Webcam Chat Satellite Internet QuickBooks Advice international calling cards international phone cards
Submit Your Blog Feedback Request Article Print Email

Introduction to DOM4J

Author : SriHari
Topic : java  
Pages :

Introduction

dom4j is an easy to use, open source library 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 a highly flexible, high-performance, and memory-efficient implementations of this XML framework. dom4j can ease the hardships of XML-based Java application development. We will see some of the features and code samples to better understand parsing XML using dom4j.

Features of DOM4J

  • DOM4J has full support for Java Collection Framework.
  • Fully supports JAXP, SAX, DOM and XSLT.
  • Parsing large XML documents with little memory overhead.
  • Support for XPath, Event based processing mode to support for massive document or XML streams.

Now we will proceed with our article with some of the Code samples using DOM4J

Loading a XML file to a Document object using DOM4J

Sample 1: How to Load a XML file to a Document object?

							
package com.javawave.xml.parser.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

/**
 * @author dhanago
 */
public class DocumentLoader
{
   /**
    * This method is used to load the xml file to a document and return it
    *
    * @param xmlFileName is the xml file name to be loaded
    * @return Document
    */
   public static Document getDocument( final String xmlFileName )
   {
      Document document = null;
      SAXReader reader = new SAXReader();
      try
      {
         document = reader.read( xmlFileName );
      }
      catch (DocumentException e)
      {
         e.printStackTrace();
      }
      return document;
   }
}
							
						

Getting a Node Element from a XML Document object using DOM4J

Sample 2: How to get a node elements from a XML Document object using DOM4J?

Consider the following XML file. (sample.xml)

							
<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Address studentId="1001">
        <Details name="Sam" age="" sex="M" class="10" />
    </Address>
    <Address studentId="1002">
        <Details name="Krish" age="" sex="M" class="10" />
    </Address>
</Root>
							
						

Now, in the above XML if you want to take all the "Address" nodes.

Here is the xPath you have to use "//Root/Address"

See how to give this xPath in the Java code:
(note: use the above code to get the document object)

							
List<Node> nodes = document.selectNodes( L0_DOMAIN_NAME );
							
						

Iterate through the list to get the single node and take the values for further processing.

Full Code: To get node list using DOM4J:

							
package summa;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class TestDom4j
{
   /**
    * This method is used to load the xml file to a document and return it
    *
    * @param xmlFileName is the xml file name to be loaded
    * @return Document
    */
   public static Document getDocument( final String xmlFileName )
   {
      Document document = null;
      SAXReader reader = new SAXReader();
      try
      {
         document = reader.read( xmlFileName );
      }
      catch (DocumentException e)
      {
         e.printStackTrace();
      }
      return document;
   }

   /**
    * @param args
    */
   public static void main( String[] args )
   {
      String xmlFileName = "D:/validation/validator/src/summa/sample.xml";
      String xPath = "//Root/Address";
      Document document = getDocument( xmlFileName );
      List<Node> nodes = document.selectNodes( xPath );
      for (Node node : nodes)
      {
         String studentId = node.valueOf( "@studentId" );
         System.out.println( "Student Id : " + studentId );
      }
   }
}
							
						

By using the xPath, DOM4J makes our life easy and really simple to get the values from the XML file.

Getting all the Node attributes from a Node using DOM4J

Sample 3: How to get all the node attributes from a node using DOM4J?

In some cases we need to get all the node attributes to parse through.
The below method will help us in doing so.

							
   private List<Attribute> getNodeAttributes( Node node )
   {
      Element e = (Element) node;
      List<Attribute> list = e.attributes();
      for (Attribute attribute : list)
      {
         String name = attribute.getName();
         System.out.println( "Name : " + name );
      }
      return list;
   }
							
						

Submit Your Blog Feedback Request Article Print Email

Java / J2EE Tutorials

Spring Framework

Hibernate Framework

JSF Framework

Struts Framework

Java Server Pages(JSP)

Servlets

Java / J2EE Design Patterns

SCJP

SCEA


Favorites
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2009), India
javabeat | about us | useful resources
Copyright (2004 - 2009), JavaBeat