最近写的插件用到了dom4j,来读写,修改文档。用到的功能都写出来了
下面包含了两个程序 WriteFriendsInfo,和 ReadFriendsInfo。
1.WriteFriendsInfo 包含了新建xml和修改,删除xml节点的方法
一些要点:
WriteFriendsInfo 中修改节点采用的方式是删除节点,然后把新的节点写进去。在不对读写要求很大的情况下,这样做比较简单。
还有就是中文的读写问题:
写入带中文的 Document 到xml文本
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
output = new XMLWriter(new FileWriter(new File("/kb2.xml")),format);
读带中文的xml文本:
我原来是这么写的:document = saxReader.read(new File("/kb2.xml"));
结果一遇到中文就报错,用FileReader可以免去很多字符问题:
FileReader fr= new FileReader(new File("/kb2.xml"));
document = saxReader.read(fr);
2.ReadFriendsInfo 的作用是读xml数据放入自定义的数据结构。
读节点很简单充分体现了 dom4j的智能化
package com.soulnew.netbeansmessage;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import org.dom4j.io.OutputFormat;
public class WriteFriendsInfo{
String filename="/kb2.xml";
public void setFilename(String filename) {
this.filename = filename;
}
public String getFilename() {
return filename;
}
public WriteFriendsInfo(){
}
public void createDocument(){
Document document = DocumentHelper.createDocument();
Element catalogElement = document.addElement("friends");
Element fmessage=catalogElement.addElement("message");
Element image=fmessage.addElement("image");
Element name");
Element gtalk=fmessage.addElement("gtalk");
Element msn=fmessage.addElement("msn");
Element qq=fmessage.addElement("qq");
Element address=fmessage.addElement("address");
fmessage=catalogElement.addElement("message");
saveXml(document);
}
public void delInfo(FriendsInfo fi){
org.dom4j.Document document=null;
org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
try{
document = (new ReadFriendsInfo(filename)).getDocument();
}catch(Exception e){
e.printStackTrace();
}
List list = document.selectNodes("/friends/message/name");
Iterator iter = list.iterator();
while(iter.hasNext()){
System.out.print("###########/n");
if(iter.hasNext()){
Element ownerElement = (Element)iter.next();
if(ownerElement.getText().equals(fi.getName()))
ownerElement.getParent().getParent().remove(ownerElement.getParent());
}
}
saveXml(document);
}
public void changeXml(FriendsInfo fi){
org.dom4j.Document document=null;
org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
try{
// FileReader fr= new FileReader(new File(filename));
document = (new ReadFriendsInfo(filename)).getDocument();
}catch(Exception e){
e.printStackTrace();
}
List list = document.selectNodes("/friends/message/name");
Iterator iter = list.iterator();
while(iter.hasNext()){
System.out.print("###########/n");
if(iter.hasNext()){
Element ownerElement = (Element)iter.next();
if(ownerElement.getText().equals(fi.getName()))
ownerElement.getParent().getParent().remove(ownerElement.getParent());
}
}
//
saveXml(document);
addXml(fi);
}
public void addXml(FriendsInfo fi){
delInfo(fi);
org.dom4j.Document document=null;
org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
try{
// FileReader fr= new FileReader(new File(filename));
document = (new ReadFriendsInfo(filename)).getDocument();
}catch(Exception e){
e.printStackTrace();
}
Element catalogElement = document.getRootElement();
Element fmessage=catalogElement.addElement("message");
Element Element name");
addEleText(name,fi.getName());
Element gtalk=fmessage.addElement("gtalk");
addEleText(gtalk,fi.getGtalk());
Element msn=fmessage.addElement("msn");
addEleText(msn,fi.getMSN());
Element qq=fmessage.addElement("qq");
addEleText(qq,fi.getQQ());
Element address=fmessage.addElement("address");
addEleText(address,fi.getAddress());
Element image=fmessage.addElement("image");
addEleText(image,fi.getImage());
saveXml(document);
}
public void addEleText(Element el,String text){
if(text!=null)
el.addText(text);
}
public void saveXml(Document document ){
try{
XMLWriter output = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
String xmlpath= getClass().getResource(filename).toString();
if(xmlpath.indexOf("file:")!=-1)
xmlpath=xmlpath.substring(6,xmlpath.length());
xmlpath=changeSpace(xmlpath);
output = new XMLWriter(new FileWriter(new File(xmlpath)),format);
output.write(document);
output.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public void reBuildXml(){
java.lang.String filename = "/kb2.xml";
createDocument();
// addXml(createtmepinfo());
addXml(createtmepinfo1());
}
public static void main(String[] argv){
java.lang.String filename = "/kb2.xml";
com.soulnew.netbeansmessage.WriteFriendsInfo dom4j = new com.soulnew.netbeansmessage.WriteFriendsInfo();
// dom4j.createDocument();
// dom4j.addXml(dom4j.createtmepinfo());
// dom4j.addXml(dom4j.createtmepinfo1());
// // dom4j.formatXMLFile(filename);
// //dom4j.changeXml(dom4j.createtmepinfo1(),filename);
// // dom4j.delInfo(dom4j.createtmepinfo1(), filename);
dom4j.reBuildXml();
dom4j.printFile(filename);
}
public FriendsInfo createtmepinfo1(){
FriendsInfo friendsinfo=new FriendsInfo("liuke","Chendu");
friendsinfo.setImage("/images/text.png");
friendsinfo.setQQ("41895555");
friendsinfo.setMSN("soulnew@gmail.com");
return friendsinfo;
}
public FriendsInfo createtmepinfo(){
FriendsInfo friendsinfo=new FriendsInfo("王","ShiChuan");
friendsinfo.setQQ("1235551");
friendsinfo.setImage("/images/text.png");
return friendsinfo;
}
public void printFile(String filename){
try {
String xmlpath= getClass().getResource(filename).toString();
if(xmlpath.indexOf("file:")!=-1)
xmlpath=xmlpath.substring(6,xmlpath.length());
xmlpath=changeSpace(xmlpath);
FileReader fr= new FileReader(xmlpath);
BufferedReader reader = new BufferedReader(fr);
String s;
while( (s = reader.readLine()) != null ) {
System.out.println(s);
}
}catch(IOException e) {
System.out.println(e);
}
}
public String changeSpace(String string){
StringBuffer sb=new StringBuffer(string);
int pos=sb.indexOf("%20");
while(pos!=-1){
sb.replace(pos, pos+3, "/u0020");
pos=sb.indexOf("%20");
}
return sb.toString();
}
}
///
package com.soulnew.netbeansmessage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.Format;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
/**
*
* @author soulnew@gmail.com
* @ blog: http://souljava.blog.163.com
*/
public class ReadFriendsInfo {
org.dom4j.Document document=null;
/** Creates a new instance of mydom4j */
public ReadFriendsInfo(){
this("/kb2.xml");
}
public ReadFriendsInfo(String filename) {
org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
try{
FileReader fr= new FileReader(new File("/kb2.xml"));
document = saxReader.read(fr);
}catch (DocumentException ex) {
WriteFriendsInfo wi=new WriteFriendsInfo();
wi.reBuildXml();
new ReadFriendsInfo(filename);
} catch(IOException e){
WriteFriendsInfo wi=new WriteFriendsInfo();
wi.reBuildXml();
new ReadFriendsInfo(filename);
// e.printStackTrace();
}
}
public Vector getInfo(){
Element root = document.getRootElement();
Vector v=new Vector();
for (Iterator i = root.elementIterator("message"); i.hasNext();){
Element foo = (Element) i.next();
FriendsInfo fi=new FriendsInfo();
fi.setName(foo.elementText("name"));
if(foo.elementText("name")==null)
continue;
fi.setGtalk(foo.elementText("gtalk"));
fi.setAddress(foo.elementText("address"));
fi.setImage(foo.elementText("image"));
fi.setQQ(foo.elementText("qq"));
fi.setMSN(foo.elementText("msn"));
fi.setImage(fi.getImage());
v.add(fi);
}
return v;
}
public static void main(String arge[]) {
ReadFriendsInfo ri=new ReadFriendsInfo("/kb2.xml");
}
}