Parsing XML
One 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; } }
Using Iterators
A document can be navigated using a variety of methods that return standard Java Iterators. For example
public void bar(Document document) throws DocumentException { Element root = document.getRootElement(); // iterate through child elements of root for ( Iterator i = root.elementIterator(); i.hasNext(); ) { Element element = (Element) i.next(); // do something } // iterate through child elements of root with element name "foo" for ( Iterator i = root.elementIterator( "foo" ); 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 } }
Powerful Navigation 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.
public void bar(Document document) { List list = document.selectNodes( "//foo/bar" ); Node node = document.selectSingleNode( "//foo/bar/author" ); String name = node.valueOf( "@name" ); }
For example if you wish to find all the hypertext links in an XHTML document the following code would do the trick.
public void findLinks(Document document) throws DocumentException { List list = document.selectNodes( "//a/@href" ); for (Iterator iter = list.iterator(); iter.hasNext(); ) { Attribute attribute = (Attribute) iter.next(); String url = attribute.getValue(); } }
If you need any help learning the XPath language we highly recommend the Zvon tutorial which allows you to learn by example.
Fast Looping
If 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. For example
public void treeWalk(Document document) { treeWalk( document.getRootElement() ); } public void treeWalk(Element element) { for ( int i = 0, size = element.nodeCount(); i < size; i++ ) { Node node = element.node(i); if ( node instanceof Element ) { treeWalk( (Element) node ); } else { // do something.... } } }
Creating a new XML document
Often 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; } }
Writing a document to a file
A 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.
import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class Foo { public void write(Document document) throws IOException { // lets write to a file 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 ); } }
Converting to and from Strings
If 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 XSLT
Applying 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.
import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import org.dom4j.Document; import org.dom4j.io.DocumentResult; import org.dom4j.io.DocumentSource; public class Foo { 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 ) ); // now lets 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(); return transformedDoc; } }
相关推荐
DOM4J是一个由dom4j.org开发的开源XML解析包,专为Java平台设计,它不仅支持DOM、SAX和JAXP标准,还巧妙地融入了Java集合框架,使其成为Java开发者在处理XML数据时的强大工具。DOM4J的最大亮点在于其简洁易用的API...
在本教程中,我们将深入探讨如何使用DOM4J解析(读取)XML节点数据,不受XML层级的限制。 首先,确保你已经下载了必要的依赖,即DOM4J库。通常,这将是一个名为`dom4j-x.x.x.jar`的文件,其中x.x.x是DOM4J的版本号...
四、DOM4J处理循环节点 在处理包含循环节点的XML时,可以使用迭代器或XPath查询来遍历这些节点。例如,遍历所有`<item>`元素: ```java Element root = document.getRootElement(); List<Element> items = root....
在项目中使用DOM4J时,只需将相应的jar包(如dom4j-1.6.1.jar、dom4j-2.0.2.jar或dom4j-2.1.1.jar)导入到类路径中,即可开始利用其功能处理XML文档。导入后,可以按照DOM4J提供的API进行编程,快速实现XML的读写...
### XML解析技术DOM4J解析 #### DOM4J解析概览 DOM4J是一种高性能、功能强大且极其易于使用的Java XML API,它最初是作为JDOM的一个智能分支发展起来的,自2000年下半年开始持续开发。DOM4J不仅在功能上超越了基本...
**DOM4J——XML解析库详解** XML(eXtensible Markup Language)作为一种标记语言,广泛应用于数据交换、配置文件和文档存储等领域。在Java环境中,解析XML文档时,我们通常会遇到各种库,其中DOM4J是一个非常流行...
使用 dom4j 解析 XML dom4j 解析 XML dom4j解析xml
DOM4J作为XML解析工具,其核心功能包括: 1. **解析XML文档**:DOM4J可以使用SAX或DOM方式解析XML文件。SAX是事件驱动的解析器,适用于大文件,而DOM解析会将整个文档加载到内存,适合小文件。DOM4J的灵活性在于,...
- **dom4j+jaxen操作XML**:首先使用dom4j解析XML,然后通过jaxen创建XPath对象并执行查询。查询结果可以是元素、属性或其他XML节点,根据需要进行进一步处理。 例如,以下代码展示了如何使用dom4j和jaxen解析XML...
本文将深入探讨DOM、DOM4J和SAX三种常用的XML解析方法,并结合具体的实例进行详细讲解。 首先,DOM(Document Object Model)是W3C组织推荐的一种XML解析标准。它将整个XML文档加载到内存中,构建一个树形结构,...
为了使用这个库,你需要将该jar包添加到项目的类路径中,然后就可以通过DOM4J提供的类和方法来解析和操作XML文件了。 总之,DOM4J是一个功能强大的XML处理库,无论是在小型项目还是大型系统中,都能发挥其优势,...
通过上述内容,我们了解了dom4j的基本用法,包括解析、遍历、修改和序列化XML文档,以及使用XPath进行节点查询。在实际开发中,这些功能可以极大地简化XML处理任务。尽管dom4j-1.6.1.jar是一个较旧的版本,但其核心...
接下来,我们来看看如何使用DOM4J来解析和校验XML文档。DOM4J提供了一种简单的方法来加载XML和XSD文件,然后使用Schema类进行校验。以下是一个基本的步骤概述: 1. 加载XSD文件:使用DOM4J的DocumentHelper类的...
本篇文章将详细介绍如何使用DOM(Document Object Model)和DOM4j这两种流行的方法来解析XML文件。 首先,DOM是一种标准的W3C推荐的解析XML的方法,它将整个XML文档加载到内存中,形成一个树形结构,便于程序进行...
常见的 XML 解析器有 DOM、SAX、JDOM 和 DOM4J 等。每种解析器都有其特点和优缺,选择合适的解析器对应用程序的性能和开发效率有很大影响。 1. DOM 解析器 DOM(Document Object Model)是 W3C 官方标准,用于表示...
使用DOM4J解析XML** 解析XML文件时,DOM4J提供了两种主要的方式:DOM阅读器和SAX阅读器。 - **DOM阅读器**:首先,通过`DocumentFactory.createDocument()`方法创建一个`Document`对象,然后使用`read()`方法读取...
### DOM4J解析XML知识点详解 #### 一、DOM4J简介 DOM4J是一个Java库,用于处理XML文档。它的设计目标是为了提供一个简单、易于使用的API来处理XML文件,同时保持性能上的优势。与Java标准库中的DOM实现相比,DOM4J...
在提供的文件中,"beans.xml"可能是一个包含Spring框架配置的文件,而"dom4j_output.xml"可能是使用DOM4J解析并修改后输出的结果。"dom4j"可能包含了DOM4J库的jar文件或相关源代码。通过DOM4J解析这些文件,你可以...