
/*

聽*聽鍒涘緩鏃ユ湡聽2005-2-15

聽*/

package聽com.tiantian.xmlguestbook;//com.tiantian


import聽org.jdom.Document;

import聽org.jdom.Element;

import聽org.jdom.JDOMException;

import聽org.jdom.input.SAXBuilder;

import聽org.jdom.output.*;

import聽java.io.*;


/**

聽*聽@author聽tiantian

聽*/

public聽class聽XMLguestbookBean聽{


聽聽聽聽public聽static聽void聽main(String[]聽args)聽{

聽聽聽聽聽聽聽聽XMLguestbookBean聽exam聽=聽new聽XMLguestbookBean();//澹版槑XMLguestbookBean鐨勪竴涓疄渚?br />

聽聽聽聽聽聽聽聽exam.addmessage("james",聽"202.11.223.22",聽"hahah");//澧炲姞涓涓暀璦錛屾祴璇?br />

聽聽聽聽}


聽聽聽聽public聽Element聽root聽=聽null;//瀹氫箟鏍?br />


聽聽聽聽public聽XMLguestbookBean()聽{

聽聽聽聽聽聽聽聽//絀虹殑鏋勯犲嚱鏁?br />

聽聽聽聽}


聽聽聽聽private聽void聽addmessage(String聽name,聽String聽ip,聽String聽content)聽{

聽聽聽聽聽聽聽聽try聽{

聽聽聽聽聽聽聽聽聽聽聽聽SAXBuilder聽sb聽=聽new聽SAXBuilder();

聽聽聽聽聽聽聽聽聽聽聽聽Document聽doc聽=聽sb.build("guestbook.xml");

聽聽聽聽聽聽聽聽聽聽聽聽root聽=聽doc.getRootElement();//鍙栧緱鏍瑰厓绱?br />

聽聽聽聽聽聽聽聽聽聽聽聽Element聽newMessage聽=聽new聽Element("message");

聽聽聽聽聽聽聽聽聽聽聽聽Element聽newName聽=聽new聽Element("name");

聽聽聽聽聽聽聽聽聽聽聽聽Element聽newContent聽=聽new聽Element("content");

聽聽聽聽聽聽聽聽聽聽聽聽Element聽newIp聽=聽new聽Element("ip");

聽聽聽聽聽聽聽聽聽聽聽聽newName.setText(name);聽//濉叆鍚嶅瓧

聽聽聽聽聽聽聽聽聽聽聽聽newContent.setText(content);//濉叆璁劇疆

聽聽聽聽聽聽聽聽聽聽聽聽newIp.setText(ip);//濉叆IP

聽聽聽聽聽聽聽聽聽聽聽聽newMessage.addContent(newName);//娣誨姞鍚嶅瓧鍒癿essage

聽聽聽聽聽聽聽聽聽聽聽聽newMessage.addContent(newIp);//娣誨姞ip鍦板潃鍒癿essage

聽聽聽聽聽聽聽聽聽聽聽聽newMessage.addContent(newContent);//娣誨姞鐣欒█鍐呭鍒癿essage

聽聽聽聽聽聽聽聽聽聽聽聽root.addContent(newMessage);//鎶妋essage娣誨姞鍒版牴涓?br />

聽聽聽聽聽聽聽聽聽聽聽聽Format聽format聽=聽Format.getCompactFormat();

聽聽聽聽聽聽聽聽聽聽聽聽format.setEncoding("gb2312");聽//璁劇疆xml鏂囦歡鐨勫瓧絎︿負gb2312

聽聽聽聽聽聽聽聽聽聽聽聽format.setIndent("聽聽聽聽");

聽聽聽聽聽聽聽聽聽聽聽聽XMLOutputter聽XMLOut聽=聽new聽XMLOutputter(format);//鍦ㄥ厓绱犲悗鎹㈣錛屾瘡涓灞傚厓绱犵緝鎺掑洓鏍?br />

聽聽聽聽聽聽聽聽聽聽聽聽XMLOut.output(doc,聽new聽FileOutputStream("guestbook.xml"));//杈撳嚭鍒版枃浠?br />

聽聽聽聽聽聽聽聽}聽catch聽(FileNotFoundException聽e)聽{

聽聽聽聽聽聽聽聽聽聽聽聽//聽鏂囦歡鏈壘鍒?br />

聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();

聽聽聽聽聽聽聽聽}聽catch聽(JDOMException聽e)聽{

聽聽聽聽聽聽聽聽聽聽聽聽//聽JDOM寮傚父

聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();

聽聽聽聽聽聽聽聽}聽catch聽(IOException聽e)聽{

聽聽聽聽聽聽聽聽聽聽聽聽//聽IO寮傚父

聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();

聽聽聽聽聽聽聽聽}


聽聽聽聽}

}

