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.
also read:
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.
Sample 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; }
Creating a new document from the scratch using dom4j
Now, Let us see how can we create a new document from the scratch using dom4j.
Sample 4: From the below code we can very well understand how easy it is to create a document from the scratch using dom4j.
/** * This method is used to return a newly created document. * * @return */ public Document buildDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement( "ROOT" ); Element author1 = root.addElement( "AUTHOR" ).addAttribute( "name", "Muthu" ).addAttribute( "location", "India" ).addText( "Javawave.blogspot.com" ); Element author2 = root.addElement( "AUTHOR" ).addAttribute( "name", "Krish" ).addAttribute( "location", "India" ).addText( "Javabeat.net" ); return document; }
Writing the XML document created using DOM4J to a file
Sample 5: More io Operations, Now let us see how can we write the created document to a file?
/** * This method is used to write a document to a file * * @param document * @throws IOException */ public void write( Document document ) throws IOException { // write to a file output.xml XMLWriter writer = new XMLWriter( new FileWriter( "output.xml" ) ); writer.write( document ); writer.close(); // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); writer = new XMLWriter( System.out, format ); writer.write( document ); // Compact format to System.out format = OutputFormat.createCompactFormat(); writer = new XMLWriter( System.out, format ); writer.write( document ); }
In case of Pretty print, the result will be as below:
<ROOT> <AUTHOR name="Muthu" location="India">Javawave.blogspot.com</AUTHOR> <AUTHOR name="Krish" location="India">Javabeat.net</AUTHOR> </ROOT>
In case of Compact format, the result will be as below:
<?xml version="1.0" encoding="UTF-8"?> <ROOT><AUTHOR name="Muthu" location="India">Javawave.blogspot.com</AUTHOR><AUTHOR name="Krish" location="India">Javabeat.net</AUTHOR></ROOT>
Converting from String to Document and Document to String using DOM4J:
It is always good to know how to convert a XML string to a Document object and a Document object to a string. This is very very easy in DOM4J. Let us see how we can do this.
Sample 6: Document to String using DOM4J
/** * This method is used to convert a Document to a String * * @param document - Document object which has to be converted to a * string object * @return */ public String convertDocument2String( Document document ) { String xml = null; xml = document.asXML(); return xml; }
Sample 7: String to Document using DOM4J
Many cases when we work with XML we also have to deal with XSLT. XSLT is mainly used to style the document. In such situations we should know how should we apply XSLT on a Document, Means we should know how to use TransformerFactory factory. The below shown example (Sample 8:) uses JAXP API (supports any XSLT engine such as Xalan or SAXON). This is just a short of Quick Tips for Java developers who want to parse a XML file using Java. Dom4J makes life easy in parsing the XML document for Java developers. By giving this information I conclude this article saying “DOM4J is the best bet for XML parsing in Java”/**
* This method is used to convert a XML String to a Document object
*
* @param xml - XML string which has to be converted to a Document object
* @return
*/
public Document convertString2Document( String xml )
{
Document document = null;
try
{
document = DocumentHelper.parseText( xml );
}
catch (DocumentException e)
{
e.printStackTrace();
}
return document;
}
Applying XSLT on a Document Object
Sample 8: Applying XSLT on a Document Object
/**
* This method is used to Apply XSLT on a Document
*
* @param document - document object to which the style sheet has to be
* applied
* @param xslt - style sheet
* @return - document
* @throws Exception
*/
public Document styleDocument( Document document, String xslt )
{
// create an instance of TransformerFactory
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
// initialize Transformer
Transformer transformer = null;
try
{
transformer = transformerFactory.newTransformer( new StreamSource(
xslt ) );
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
}
Document finalDoc = null;
if (null != transformer)
{
DocumentSource source = new DocumentSource( document );
DocumentResult result = new DocumentResult();
// transform the source to the document result
try
{
transformer.transform( source, result );
}
catch (TransformerException e)
{
e.printStackTrace();
}
// return the transformed document
finalDoc = result.getDocument();
}
return finalDoc;
}
Summary