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());
}