Feng.Li's Java See

          抓緊時間,大步向前。
          隨筆 - 95, 文章 - 4, 評論 - 58, 引用 - 0
          數據加載中……

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

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

          XMLBuilder.java

          ?用于創建DOM,Root結點

          /********************************************************************
          ?* 項目名稱????:rochoc???<p>
          ?* 包名稱? ????:rochoc.xml.oper?<p>
          ?* 文件名稱????:XmlBuilder???<p>
          ?* 編寫者 ????:luoc????<p>
          ?* 編寫日期????:2005-6-22????<p>
          ?* 程序功能(類)描述?:?根據傳入的XML文件生成Document和root結點<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>
          ?* 類描述:根據傳入的XML文件生成Document和root結點 <p>
          ?* 編寫者 :luoc<p>
          ?* 編寫日期 :2005-6-22<p>
          ?* 主要public成員變量:<p>
          ?* 主要public方法:?? <p>
          ?**/

          public class XmlBuilder
          {
          ??? /**
          ???? *構造函數說明:?????? <p>
          ???? *參數說明:@param path?? <p>
          ??? **/
          ??? public XmlBuilder(String path)
          ??? {
          ??????? this.path=path;
          ??????? init();
          ??? }
          ???
          ??? /**
          ??? * 方法名稱:init<p>
          ??? * 方法功能:初始化函數<p>
          ??? * 參數說明: <p>
          ??? * 返回:void <p>
          ??? * 作者:luoc
          ??? * 日期:2005-6-22
          ??? **/
          ??? public void init()
          ??? {
          ??????? buildDocument();
          ??????? buildRoot();
          ??? }
          ???
          ??? /**
          ??? * 方法名稱:buildDocument<p>
          ??? * 方法功能:將XML文件生成Document <p>
          ??? * 參數說明: <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的根結點<p>
          ??? * 參數說明: <p>
          ??? * 返回:void <p>
          ??? * 作者:luoc
          ??? * 日期:2005-6-22
          ??? **/
          ??? private void buildRoot()
          ??? {
          ??????? root=doc.getDocumentElement();
          ??? }
          ???
          ??? /**
          ???? * @return 返回 doc。
          ???? */
          ??? public Document getDoc()
          ??? {
          ??????? return doc;
          ??? }
          ??? /**
          ???? * @param doc 要設置的 doc。
          ???? */
          ??? public void setDoc(Document doc)
          ??? {
          ??????? this.doc = doc;
          ??? }
          ??? /**
          ???? * @return 返回 path。
          ???? */
          ??? public String getPath()
          ??? {
          ??????? return path;
          ??? }
          ??? /**
          ???? * @param path 要設置的 path。
          ???? */
          ??? public void setPath(String path)
          ??? {
          ??????? this.path = path;
          ??? }
          ??? /**
          ???? * @return 返回 root。
          ???? */
          ??? public Element getRoot()
          ??? {
          ??????? return root;
          ??? }
          ??? /**
          ???? * @param root 要設置的 root。
          ???? */
          ??? public void setRoot(Element root)
          ??? {
          ??????? this.root = root;
          ??? }
          ??? /*全局變量*/
          ??? private String path=null;//xml文件路徑
          ??? private Document doc=null;//xml文件對應的document
          ??? private Element root=null;//xml文件的根結點
          ??? private Logger logger=Logger.getLogger(getClass().getName());
          }

          posted on 2006-12-05 23:42 小鋒 閱讀(809) 評論(0)  編輯  收藏 所屬分類: J2EE

          主站蜘蛛池模板: 龙井市| 蓬安县| 盐城市| 汤阴县| 南通市| 扎兰屯市| 开平市| 板桥市| 西青区| 麦盖提县| 札达县| 巴南区| 宿州市| 海安县| 林周县| 仁寿县| 杭锦后旗| 鹤山市| 青海省| 东兰县| 抚宁县| 勃利县| 喀什市| 九台市| 宁乡县| 湘西| 乌海市| 石狮市| 长垣县| 宜春市| 襄樊市| 白朗县| 萨嘎县| 剑阁县| 周至县| 上林县| 雅江县| 垦利县| 穆棱市| 富蕴县| 眉山市|