Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Knowledge Management (in the Web)

Dom4j
Duration: 2hrs

Tutorial

[Tutorial] dom4j.doc

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. Main features: designed for the Java platform with full support for the Java Collections Framework full support for JAXP, TrAX, SAX, DOM, and XSLT fully integrated XPath support for easy navigation of XML documents event based processing mode to support for massive documents or XML streams based on Java interfaces for flexible plug and play implementations support for XML Schema Data Type support

Deployment Instructions
In order to deploy dom4j from within the Netbeans environment, you need to perform the following steps: 1. Download the current release of dom4j. 2. Create a new Netbeans library (Tools > Libraries). 3. Name the library "dom4j" and associate its jar and javadocs. 4. Create a new Netbeans project. 5. Include the library in the newly created Netbeans project. 6. Now you are ready to use the dom4j API for processing XML vocabularies!

Tutorial Topics
This tutorial introduces you to the basic dom4j functionalities, covering the following topics: Creating a new XML document Writing a document to a file Parsing XML Converting to and from Strings Using Iterators Navigating with XPath Styling a Document with XSLT

Creating a new XML document


You will often need to create a new document from scratch. Here's an example of creating a new XML document that describes two books: // Creating a new XML document public Document createDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement("books"); Element book1 = root.addElement("book").addAttribute("category", "history"); book1.addElement("title").addText("Cultural Atlas of Ancient Egypt"); book1.addElement("author").addText("John Baines"); book1.addElement("author").addText("Jaromir Malek"); book1.addElement("price").addText("23.99"); Element book2 = root.addElement("book").addAttribute("category", "science"); book2.addElement("title").addText("The Einstein Theory of Relativity"); book2.addElement("author").addText("Hendrik Antoon Lorentz"); book2.addElement("rating").addText("4");

[Tutorial] dom4j.doc

book2.addElement("price").addText("3.99"); return document; }

Writing a document to a file


There are 2 ways of writing an XML to a file. The first way is very quick and easy: FileWriter out = new FileWriter("foo.xml"); document.write(out); The second way applies the XMLWriter class for changing the format of the output, such as pretty printing, or working with Writer objects or OutputStream objects as the destination: // Writing a document to a file with name "fileName" public void write(Document document, String fileName) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileWriter(fileName), format); writer.write( document ); writer.close(); }

Parsing XML
This is how an existing XML document can be parsed in dom4j: // Parsing XML document residing in path "filePath" public Document parse(String filePath) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(string); return document; }

Converting to and from Strings


A dom4j Document object can be turned into XML text via the asXML() method: Document document = ...; String text = document.asXML(); On the other hand, an XML fragment can be parsed back into a Document again using the helper method DocumentHelper.parseText(): String text = "<person> <name>James</name> </person>"; Document document = DocumentHelper.parseText(text);

Using Iterators
A document can be navigated using a variety of methods that return standard Java Iterators. For example: Element root = document.getRootElement(); // iterate through child elements of root node for (Iterator i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); // do something }

[Tutorial] dom4j.doc

// iterate through child elements of root with element name "book" for (Iterator i = root.elementIterator("book"); i.hasNext();) { Element foo = (Element) i.next(); // do something } // iterate through attributes of root for (Iterator i = root.attributeIterator(); i.hasNext();) { Attribute attribute = (Attribute) i.next(); // do something }

Navigating with XPath


In dom4j XPath expressions can be evaluated on the Document or on any Node in the tree (such as Attribute, Element or ProcessingInstruction). This allows complex navigation throughout the document with a single line of code. For example, all book categories in the XML document are retrieved like this: List list = document.selectNodes( "//book/@category" ); for (Iterator iter = list.iterator(); iter.hasNext(); ) { Attribute attribute = (Attribute) iter.next(); String category = attribute.getValue(); // do something with the categories } Other examples: // Retrieve list of nodes List list = document.selectNodes( "//foo/bar" ); // Retrieve a single node Node node = document.selectSingleNode( "//foo/bar/author" ); // Retrieve an attribute value String name = node.valueOf( "@name" );

Styling a Document with XSLT


Applying XSLT on a Document is quite straightforward with dom4j using Suns JAXP API. This allows working against any XSLT engine such as Xalan or SAXON. Here is an example of using JAXP to create a transformer and then applying it to a Document. public Document styleDocument(Document document, String stylesheet) throws Exception { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheet)); // style the given document DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // return the transformed document Document transformedDoc = result.getDocument();

[Tutorial] dom4j.doc

return transformedDoc; } Document document = foo.parse("amazon-extended.xml"); Document result = foo.styleDocument(document, "all-products-price.xsl"); System.out.println(result.asXML());

[Tutorial] dom4j.doc

You might also like