import org.dom4j.Document; //導(dǎo)入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 類創(chuàng)建一個(gè)文檔實(shí)例。DocumentHelper 是生成XML 文檔節(jié)點(diǎn)的dom4j API 工廠類。
Document document = DocumentHelper.createDocument();
//使用addElement() 方法創(chuàng)建根元素catalog 。addElement() 用于向XML 文檔中增加元素。
Element catalogElement = document.addElement("catalog");
//在catalog 元素中使用addComment() 方法添加注釋“An XML catalog”。
catalogElement.addComment("An XML Catalog");
//在catalog 元素中使用addProcessingInstruction() 方法增加一個(gè)處理指令。
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() 方法設(shè)置article 元素的文本
titleElement.setText("Java configuration with XML Schema");
//向article元素中添加author元素
Element authorElement=articleElement.addElement("author");
//向author元素中添加firstname,lastname元素,并用setText()設(shè)置他們的文本
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 返回操作結(jié)果, 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();
/** 執(zhí)行成功,需返回1 */
returnValue = 1;
return returnValue;
}
/**
* 修改XML文檔,并按指定字符集輸出
* @param inputXml
* @param modified_filename 修改后的文件名(含絕對路徑)
* @return 返回操作結(jié)果, 0表失敗, 1表成功
*/
public int modifyDocument(File inputXml,String modified_filename){ int returnValue = 0;
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
//根據(jù)XPath語法查詢結(jié)點(diǎn)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("http://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("http://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("http://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("http://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());
}
/** 執(zhí)行成功,需返回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文件默認(rèn)狀態(tài)和類文件在同一個(gè)目錄下
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());}
}
}
|