dom4j 是一种解析 XML 文档的开放源代码 XML 框架,与 W3C DOM API 相比,使用 dom4j 所包含的解析器的好处是 dom4j 拥有本地的 XPath 支持.DOM 解析器不支持使用 XPath 选择节点.
xml 代码
该解析器可以从 http://dom4j.org/ 获取.
java 代码
java 代码
- package com.nikee.dom4j;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.XMLWriter;
- import java.io.*;
- public class XmlDom4J{
- public void generateDocument(){
- //使用 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");
- Element articleElement=journalElement.addElement("article");
- articleElement.addAttribute("level", "Intermediate");
- articleElement.addAttribute("date", "December-2001");
- Element titleElement=articleElement.addElement("title");
- titleElement.setText("Java configuration with XML Schema");
- Element authorElement=articleElement.addElement("author");
- Element firstNameElement=authorElement.addElement("firstname");
- firstNameElement.setText("Marcello");
- Element lastNameElement=authorElement.addElement("lastname");
- lastNameElement.setText("Vitaletti");
- //可以使用 addDocType() 方法添加文档类型说明
- //这样就向 XML 文档中增加文档类型说明:
- document.addDocType("catalog","nikee","file://c:/Dtds/catalog.dtd");
- try{
- //XMLWriter output = new XMLWriter(new FileWriter( new File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml")));
- FileOutputStream fos=new FileOutputStream("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml");
- OutputFormat of=new OutputFormat(" ", true);
- XMLWriter xw=new XMLWriter(fos, of);
- xw.write( document );
- xw.close();
- }
- catch(IOException e)
- {
- System.out.println(e.getMessage());
- }
- }
- public static void main(String[] argv){
- XmlDom4J dom4j=new XmlDom4J();
- dom4j.generateDocument();
- }
- }
xml 代码
xml 代码
- <!--sp-->xml version="1.0" encoding="UTF-8"?>
- <!--CTYPE catalog PUBLIC "nikee" "file://c:/Dtds/catalog.dtd"</sp-->>
- <catalog>
- <!--An XML Catalog--><!--sp-->target text?>
- <journal title="XML Zone" publisher="IBM developerWorks">
- <article level="Intermediate" date="December-2001">
- <title>Java configuration with XML Schematitle>
- <author>
- <firstname>Marcellofirstname>
- <lastname>Vitalettilastname>
- author>
- article>
- journal>
- catalog>
XmlDom4J.java 用于创建 XML 文档 catalog.xml
java 代码
java 代码
- package com.nikee.dom4j;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.Attribute;
- import java.util.List;
- import java.util.Iterator;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.XMLWriter;
- import java.io.*;
- import org.dom4j.DocumentException;
- import org.dom4j.io.SAXReader;
- public class Dom4Jparser{
- public Dom4Jparser(){
- //construction
- }
- public void modifyDocument(File inputXml){
- try{
- //使用 SAXReader 解析 XML 文档 catalog.xml
- SAXReader saxReader = new SAXReader();
- Document document = saxReader.read(inputXml);
- //使用 XPath 表达式从 article 元素中获得 level 节点列表。如果 level 属性值是"Intermediate"则改为"Introductory"
- 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");
- }
- //获取 article 元素列表,从 article 元素中的 title 元素得到一个迭代器,并修改 title 元素的文本
- 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");
- }
- }
- FileOutputStream fos=new FileOutputStream("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog-modified.xml");
- OutputFormat of=new OutputFormat(" ",true);
- XMLWriter xw=new XMLWriter(fos, of);
- xw.write( document );
- xw.close();
- }
- catch(DocumentException e)
- {
- e.printStackTrace();
- }
- catch(IOException e){
- e.printStackTrace();
- }
- }
- public static void main(String argv[]){
- Dom4Jparser dom4jParser=new Dom4Jparser();
- dom4jParser.modifyDocument(new File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml"));
- }
- }
xml 代码
<!--sp-->xml version="1.0" encoding="UTF-8"?> <!--<!DOCTYPE catalog PUBLIC "nikee" "file://c:/Dtds/catalog.dtd">--> <catalog> <!--An XML Catalog--><!--sp-->target text?> <journal title="XML Zone" publisher="IBM developerWorks"> <article level="Introductory" date="October-2002"> <title>Create flexible and extensible XML schematitle> <author> <firstname>Ayeshafirstname> <lastname>Maliklastname> author> article> journal> catalog>
Dom4Jparser.java用于修改 XML 文档 catalog.xml为catalog-modified.xml
问题总结:
1.当catalog.xml有<!--CTYPE catalog PUBLIC "nikee" "file://c:/Dtds/catalog.dt-->这一句,Dom4Jparser.java修改 XML 文档 catalog.xml为catalog-modified.xml时,发生异常org.dom4j.DocumentException: c Nested exception: c.
原因:我自己没有此file://c:/Dtds/catalog.dtd.
解决办法:删掉此行.
2.除了加入dom4j.jar包外,必须加入jaxen.jar包,否则会发生异常.jaxen包可在http://jaxen.org/下载.