Listing Details| ID: | 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Title: | DOM4J | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pagerank: | 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Short Description: | 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. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description: | This page attempts to survey the landscape of available XML object models and compare and contrast their features. The information in this table is correct to the best of our knowledge and we will try and keep this information as up to date as possible. If you think there's anything wrong, please let us know here.
Fast LoopingIf you ever have to walk a large XML document tree then for performance we recommend you use the fast looping method which avoids the cost of creating an Iterator object for each loop Powerful Navigation with XPathIn 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 Using IteratorsA document can be navigated using a variety of methods that return standard Java Iterators Converting to and from StringsIf you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method. Document document = ...; String text = document.asXML(); If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText() String text = "<person> <name>James</name> </person>"; Document document = DocumentHelper.parseText(text); Styling a Document with XSLTApplying XSLT on a Document is quite straightforward using the JAXP API from Sun. This allows you to work 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. Writing a document to a fileA quick and easy way to write a Document (or any Node) to a Writer is via the write() method. FileWriter out = new FileWriter( "foo.xml" ); document.write( out ); If you want to be able to change the format of the output, such as pretty printing or a compact format, or you want to be able to work with Writer objects or OutputStream objects as the destination, then you can use the XMLWriter class. Creating a new XML documentOften in dom4j you will need to create a new document from scratch. Here's an example of doing that. import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element;
public class Foo {
public Document createDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement( "root" );
Element author1 = root.addElement( "author" ) .addAttribute( "name", "James" ) .addAttribute( "location", "UK" ) .addText( "James Strachan" );
Element author2 = root.addElement( "author" ) .addAttribute( "name", "Bob" ) .addAttribute( "location", "US" ) .addText( "Bob McWhirter" );
return document; } } Parsing XMLOne of the first things you'll probably want to do is to parse an XML document of some kind. This is easy to do in dom4j. The following code demonstrates how to this. import java.net.URL;
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader;
public class Foo {
public Document parse(URL url) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(url); return document; } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Category: | XML | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Link Owner: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Date Added: | May 05, 2010 07:00:44 PM | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Number Hits: | 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| URL: | http://sourceforge.net/projects/dom4j/files/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||