dom4j使用记录

我使用的版本为: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 文档中增加文档类型说明:

<!DOCTYPE catalog SYSTEM "file://c:/Dtds/catalog.dtd">


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

XML 声明 <?xml version="1.0" encoding="UTF-8"?> 自动添加到 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<String> parse(String path) throws Exception
{
List list=this.doc.selectNodes(path);

List<String> ls=new ArrayList<String>();
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<String> ls=imp.parse("//id");
Iterator<String> it=ls.iterator();
System.out.println("******BEGIN******");
System.out.println("Nodes:");
while(it.hasNext())
{
System.out.println("------"+it.next());
}
System.out.println("*******END*******");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值