dom4j 是一种解析 XML 文档的开放源代码 XML 框架,与 W3C DOM API 相比,使用 dom4j 所包含的解析器的好处是 dom4j 拥有本地的 XPath 支持.DOM 解析器不支持使用 XPath 选择节点.
该解析器可以从 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(){
-
-
Document document = DocumentHelper.createDocument();
-
-
-
Element catalogElement = document.addElement(
"catalog"
);
-
-
-
catalogElement.addComment(
"An XML Catalog"
);
-
-
-
catalogElement.addProcessingInstruction(
"target"
,
"text"
);
-
-
-
Element journalElement = catalogElement.addElement(
"journal"
);
-
-
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"
);
-
-
-
-
document.addDocType(
"catalog"
,
"nikee"
,
"file://c:/Dtds/catalog.dtd"
);
-
try
{
-
-
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
version
=
"1.0"
encoding
=
"UTF-8"
?>
-
>
-
-
<
catalog
>
-
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(){
-
- }
-
- 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");
- }
- }
-
- 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 代码
xml 代码
-
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
-
-
-
<
catalog
>
-
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/
下载.
posted on 2007-11-02 18:00
缘来如此 阅读(6982)
评论(0) 编辑 收藏 所属分类:
XML