捕風(fēng)之巢

          統(tǒng)計

          留言簿(3)

          java友情鏈接

          閱讀排行榜

          評論排行榜

          采用org.w3c.dom進行XML操作(一)(轉(zhuǎn)載)

          采用org.w3c.dom進行XML操作(一)
          ?

          XMLBuilder.java

          ?用于創(chuàng)建DOM,Root結(jié)點

          /********************************************************************
          ?* 項目名稱????:rochoc???<p>
          ?* 包名稱? ????:rochoc.xml.oper?<p>
          ?* 文件名稱????:XmlBuilder???<p>
          ?* 編寫者 ????:luoc????<p>
          ?* 編寫日期????:2005-6-22????<p>
          ?* 程序功能(類)描述?:?根據(jù)傳入的XML文件生成Document和root結(jié)點<p>
          ?*
          ?* 程序變更日期???:
          ?* 變更作者????:
          ?* 變更說明????:
          ********************************************************************/
          package rochoc.xml.oper;

          import java.io.File;
          import java.io.IOException;

          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.ParserConfigurationException;

          import org.apache.log4j.Logger;
          import org.w3c.dom.Document;
          import org.w3c.dom.Element;
          import org.xml.sax.SAXException;

          /**
          ?* 類名:XmlBuilder? <p>
          ?* 類描述:根據(jù)傳入的XML文件生成Document和root結(jié)點 <p>
          ?* 編寫者 :luoc<p>
          ?* 編寫日期 :2005-6-22<p>
          ?* 主要public成員變量:<p>
          ?* 主要public方法:?? <p>
          ?**/

          public class XmlBuilder
          {
          ??? /**
          ???? *構(gòu)造函數(shù)說明:?????? <p>
          ???? *參數(shù)說明:@param path?? <p>
          ??? **/
          ??? public XmlBuilder(String path)
          ??? {
          ??????? this.path=path;
          ??????? init();
          ??? }
          ???
          ??? /**
          ??? * 方法名稱:init<p>
          ??? * 方法功能:初始化函數(shù)<p>
          ??? * 參數(shù)說明: <p>
          ??? * 返回:void <p>
          ??? * 作者:luoc
          ??? * 日期:2005-6-22
          ??? **/
          ??? public void init()
          ??? {
          ??????? buildDocument();
          ??????? buildRoot();
          ??? }
          ???
          ??? /**
          ??? * 方法名稱:buildDocument<p>
          ??? * 方法功能:將XML文件生成Document <p>
          ??? * 參數(shù)說明: <p>
          ??? * 返回:void <p>
          ??? * 作者:luoc
          ??? * 日期:2005-6-22
          ??? **/
          ??? private void buildDocument()
          ??? {
          ??????? DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
          ??????? try
          ??????? {
          ??????????? DocumentBuilder builder=factory.newDocumentBuilder();
          ??????????? logger.debug("Construct document builder success.");
          ??????????? doc=builder.parse(new File(path));???????????
          ??????????? logger.debug("Build xml document success.");
          ??????? }catch(ParserConfigurationException e)
          ??????? {
          ??????????? logger.error("Construct document builder error:"+e);
          ??????? }catch(SAXException e)
          ??????? {
          ??????????? logger.error("Parse xml file error:"+e);
          ??????? }catch(IOException e)
          ??????? {
          ??????????? logger.error("Read xml file error:"+e);
          ??????? }
          ??? }
          ???
          ??? /**
          ??? * 方法名稱:buildRoot<p>
          ??? * 方法功能:生成XML的根結(jié)點<p>
          ??? * 參數(shù)說明: <p>
          ??? * 返回:void <p>
          ??? * 作者:luoc
          ??? * 日期:2005-6-22
          ??? **/
          ??? private void buildRoot()
          ??? {
          ??????? root=doc.getDocumentElement();
          ??? }
          ???
          ??? /**
          ???? * @return 返回 doc。
          ???? */
          ??? public Document getDoc()
          ??? {
          ??????? return doc;
          ??? }
          ??? /**
          ???? * @param doc 要設(shè)置的 doc。
          ???? */
          ??? public void setDoc(Document doc)
          ??? {
          ??????? this.doc = doc;
          ??? }
          ??? /**
          ???? * @return 返回 path。
          ???? */
          ??? public String getPath()
          ??? {
          ??????? return path;
          ??? }
          ??? /**
          ???? * @param path 要設(shè)置的 path。
          ???? */
          ??? public void setPath(String path)
          ??? {
          ??????? this.path = path;
          ??? }
          ??? /**
          ???? * @return 返回 root。
          ???? */
          ??? public Element getRoot()
          ??? {
          ??????? return root;
          ??? }
          ??? /**
          ???? * @param root 要設(shè)置的 root。
          ???? */
          ??? public void setRoot(Element root)
          ??? {
          ??????? this.root = root;
          ??? }
          ??? /*全局變量*/
          ??? private String path=null;//xml文件路徑
          ??? private Document doc=null;//xml文件對應(yīng)的document
          ??? private Element root=null;//xml文件的根結(jié)點
          ??? private Logger logger=Logger.getLogger(getClass().getName());
          }

          posted on 2007-03-22 14:59 捕風(fēng) 閱讀(538) 評論(0)  編輯  收藏 所屬分類: xml應(yīng)用

          主站蜘蛛池模板: 土默特右旗| 昂仁县| 南投市| 台山市| 佛山市| 宝鸡市| 高安市| 德阳市| 金山区| 孙吴县| 固始县| 仁寿县| 涟源市| 淮阳县| 石首市| 东阳市| 泰和县| 金平| 墨江| 老河口市| 衡水市| 张掖市| 施秉县| 龙游县| 望都县| 镇巴县| 太和县| 遵义市| 北安市| 潢川县| 聂拉木县| 深州市| 吉林省| 铜陵市| 西峡县| 保康县| 潜江市| 烟台市| 陆丰市| 云和县| 荆门市|