來自IBM的文章,感覺不錯,剛開始使用XPath的時候,不知道還要jaxen.jar包,郁悶了好半天,呵
// 使用dom4j解析XML時,要快速獲取某個節點的數據,使用XPath是個不錯的方法,dom4j的快速手冊里也建議使用這種方式
// 執行時卻拋出以下異常:
//
// Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
// at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
// at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
// at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
//
// 不光要有dom4j這個包,還要有jaxen包:<jaxen-1.1-beta-6.jar>-238 KB,這應該是dom4j的基礎包,在dom4j的zip包的lib目錄里可以找到。即使用這個方法需要以下兩個包:
// dom4j-1.6.1.jar-306 KB
// jaxen-1.1-beta-6.jar-238 KB
修改前
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML Catalog-->
<?target instruction?>
<journal title="XML Zone"
publisher="IBM developerWorks">
<article level="Intermediate" date="December-2001">
<title>Java configuration with XML Schema</title>
<author>
<firstname>Marcello</firstname>
<lastname>Vitaletti</lastname>
</author>
</article>
</journal>
</catalog>
修改后
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML catalog-->
<?target instruction?>
<journal title="XML Zone"
publisher="IBM developerWorks">
<article level="Introductory" date="October-2002">
<title>Create flexible and extensible XML schemas</title>
<author>
<firstname>Ayesha</firstname>
<lastname>Malik</lastname>
</author>
</article>
</journal>
</catalog>
解析器
package com.Gavin.tools.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class TestDom4jChange {
public void modifyDocument(File inputXml) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
List list = document.selectNodes("//article/@level");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("Intermediate"))
attribute.setValue("Introductory");
}

list = document.selectNodes("//article/@date");
iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("December-2001"))
attribute.setValue("October-2002");
}
list = document.selectNodes("//article");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("title");
while (iterator.hasNext()) {
Element titleElement = (Element) iterator.next();
if (titleElement.getText().equals("Java configuration with XML Schema"))
titleElement.setText("Create flexible and extensible XML schema");
}
}
list = document.selectNodes("//article/author");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("firstname");
while (iterator.hasNext()) {
Element firstNameElement = (Element) iterator.next();
if (firstNameElement.getText().equals("Marcello"))
firstNameElement.setText("Ayesha");
}
}
list = document.selectNodes("//article/author");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("lastname");
while (iterator.hasNext()) {
Element lastNameElement = (Element) iterator.next();
if (lastNameElement.getText().equals("Vitaletti"))
lastNameElement.setText("Malik");
}
}
XMLWriter output = new XMLWriter(new FileWriter(new File("c:/catalog/catalog-modified.xml")));
output.write(document);
output.close();
}

catch (DocumentException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

public static void main(String[] argv) {
TestDom4jChange dom4jParser = new TestDom4jChange();
dom4jParser.modifyDocument(new File("c:/catalog/catalog.xml"));
}
}
// 使用dom4j解析XML時,要快速獲取某個節點的數據,使用XPath是個不錯的方法,dom4j的快速手冊里也建議使用這種方式
// 執行時卻拋出以下異常:
//
// Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
// at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
// at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
// at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
//
// 不光要有dom4j這個包,還要有jaxen包:<jaxen-1.1-beta-6.jar>-238 KB,這應該是dom4j的基礎包,在dom4j的zip包的lib目錄里可以找到。即使用這個方法需要以下兩個包:
// dom4j-1.6.1.jar-306 KB
// jaxen-1.1-beta-6.jar-238 KB
修改前
















修改后
















解析器





















































































