---
title: "dom4j使用记录"
author: "freeglad"
date: "2008-09-02T09:52:00Z"
tags: ["C", "C#", "C++", "IBM", "XML"]
slug: "dom4j"
draft: false
lastmod: "2008-09-02T09:52:00Z"
id: 586443
url: "https://freeglad.iteye.com/blog/586443"
---

我使用的版本为：dom4j-1.6.1  
使用时出现的异常：  
java.lang.NoClassDefFoundError: org/jaxen/JaxenException  
需要导入jaxen-1.1-beta-6.jar包。

使用 DocumentHelper 类创建一个文档实例。 DocumentHelper 是生成 XML 文档节点的 dom4j API 工厂类。  
Document document = DocumentHelper.createDocument();

使用 addElement() 方法创建根元素 catalog 。 addElement() 用于向 XML 文档中增加元素。  
Element catalogElement = document.addElement("catalog");

在 catalog 元素中使用 addComment() 方法添加注释“An XML catalog”。  
catalogElement.addComment("An XML catalog");

在 catalog 元素中使用 addProcessingInstruction() 方法增加一个处理指令。  
catalogElement.addProcessingInstruction("target","text");

在 catalog 元素中使用 addElement() 方法增加 journal 元素。

Element journalElement = catalogElement.addElement("journal");

使用 addAttribute() 方法向 journal 元素添加 title 和 publisher 属性。

journalElement.addAttribute("title", "XML Zone");  
journalElement.addAttribute("publisher", "IBM developerWorks");

向 article 元素中添加 journal 元素。

Element articleElement=journalElement.addElement("article");

为 article 元素增加 level 和 date 属性。

articleElement.addAttribute("level", "Intermediate");  
articleElement.addAttribute("date", "December-2001");

向 article 元素中增加 title 元素。

Element titleElement=articleElement.addElement("title");

使用 setText() 方法设置 article 元素的文本。

titleElement.setText("Java configuration with XML Schema");

在 article 元素中增加 author 元素。

Element authorElement=articleElement.addElement("author");

在 author 元素中增加 firstname 元素并设置该元素的文本。

Element firstNameElement=authorElement.addElement("firstname");  
firstNameElement.setText("Marcello");

在 author 元素中增加 lastname 元素并设置该元素的文本。

Element lastNameElement=authorElement.addElement("lastname");  
lastNameElement.setText("Vitaletti");

可以使用 addDocType() 方法添加文档类型说明。

document.addDocType("catalog", null,"[file://c:/Dtds/catalog.dtd]()");

这样就向 XML 文档中增加文档类型说明：

&lt;!DOCTYPE catalog SYSTEM "[file://c:/Dtds/catalog.dtd]()"&gt;

如果文档要使用文档类型定义（DTD）文档验证则必须有 Doctype。

XML 声明 &lt;?xml version="1.0" encoding="UTF-8"?&gt; 自动添加到 XML 文档中。

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
\* 查询已经存在XML文件或XML注入流，  
\* 获取指定的列，并返回列的集合。  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
package com.zjhk.dom.impl;

import java.io.File;  
import java.io.InputStream;  
import java.util.Iterator;  
import java.util.List;  
import java.util.ArrayList;  
import org.dom4j.Document;  
import org.dom4j.DocumentException;  
import org.dom4j.DocumentHelper;  
import org.dom4j.Element;  
import org.dom4j.io.SAXReader;

public class IDomImpl {

private Document doc;  
private SAXReader saxread=new SAXReader();

//生成一个空的Document  
public IDomImpl()  
{  
this.doc=DocumentHelper.createDocument();  
}

//使用file初始化Document  
public IDomImpl(File file) throws Exception  
{  
this.doc=saxread.read(file);  
}

//使用file初始化Document  
public IDomImpl(String file) throws Exception  
{  
this.doc=saxread.read(new File(file));  
}

//使用InputStream初始化Document  
public IDomImpl(InputStream is) throws Exception  
{  
this.doc=saxread.read(is);  
}

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
\* 功能：返回XPATH表达式所表示的结点值记录的集合  
\* @param path XPATH表达式  
\* @return list  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
public List&lt;String&gt; parse(String path) throws Exception  
{  
List list=this.doc.selectNodes(path);

List&lt;String&gt; ls=new ArrayList&lt;String&gt;();  
Iterator iter=list.iterator();  
while(iter.hasNext())  
{  
Element elem=(Element)iter.next();  
ls.add(elem.getText());  
}  
return ls;  
}

public static void main(String args\[]) throws Exception  
{  
IDomImpl imp=new IDomImpl("F:/xiao1.xml");  
System.out.println(imp);  
List&lt;String&gt; ls=imp.parse("//id");  
Iterator&lt;String&gt; it=ls.iterator();  
System.out.println("\*\*\*\*\*\*BEGIN\*\*\*\*\*\*");  
System.out.println("Nodes:");  
while(it.hasNext())  
{  
System.out.println("------"+it.next());  
}  
System.out.println("\*\*\*\*\*\*\*END\*\*\*\*\*\*\*");  
}  
}