import org.dom4j.Document; //导入dom4j API 类
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;
import java.io.*;
import java.util.List;
import java.util.Iterator;
class Rt
{
public Document 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");
//向journal 元素中添加article 元素,使用addAttribute()方法向article元素添加level,date属性
Element articleElement=journalElement.addElement("article");
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,lastname元素,并用setText()设置他们的文本
Element firstNameElement=authorElement.addElement("firstname");
firstNameElement.setText("Marcello");
Element lastNameElement=authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
return document;
//使用addDocType() 方法添加文档类型说明
//document.addDocType("catalog",null,"file://c:/Dtds/catalog.dtd");
/**try
{
XMLWriter output = new XMLWriter(
new FileWriter( new File("mycatalog.xml") ));
output.write( document );
output.close();
}
catch(IOException e)
{System.out.println(e.getMessage());}**/
}
/**
* 格式化XML文档,并按指定字符集输出
* @param document
* @param fileName
* @param encoding 编码格式
* @return 返回操作结果, 0表失败, 1表成功
*/
public static int saveXml(Document document,
String fileName,
String encoding)
throws UnsupportedEncodingException,
FileNotFoundException,
IOException{
int returnValue = 0;
XMLWriter output = null;
/** 格式化输出,类型IE浏览一样*/
OutputFormat format = OutputFormat.createPrettyPrint();
/** 指定XML字符集编码*/
format.setEncoding(encoding);
output = new XMLWriter(new FileOutputStream(new File(fileName)), format);
output.write(document);
output.close();
/** 执行成功,需返回1 */
returnValue = 1;
return returnValue;
}
/**
* 修改XML文档,并按指定字符集输出
* @param inputXml
* @param modified_filename 修改后的文件名(含绝对路径)
* @return 返回操作结果, 0表失败, 1表成功
*/
public int modifyDocument(File inputXml,String modified_filename){ int returnValue = 0;
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
//根据XPath语法查询结点catalog下的journal下的article的level属性
List list = document.selectNodes("/catalog/journal/article/@level" );
Iterator iter=list.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
if(attribute.getValue().equals("Intermediate")) //如果属性值是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(modified_filename) ));
output.write( document );
output.close();
}
catch(DocumentException e)
{
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
/** 执行成功,需返回1 */
returnValue = 1;
return returnValue;
}
public static void main(String[] argv)
{
Rt savedomtoxml=new Rt();
Document doc=savedomtoxml.generateDocument();
try{
int re=savedomtoxml.saveXml(doc,"savexml.xml","gb2312"); //生成的xml文件默认状态和类文件在同一个目录下
System.out.println(re);
}
catch(IOException e)
{System.out.println(e.getMessage());}
try{
int modifyre=savedomtoxml.modifyDocument(new File("H:/dom4j/rt/orginal.xml"));
System.out.println(modifyre);
}
catch(Exception e)
{System.out.println(e.getMessage());}
}
}
|