Professional Documents
Culture Documents
Dom4j: Knowledge Management (In The Web)
Dom4j: 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
[Tutorial] dom4j.doc
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; }
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 }
[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