posts - 21,  comments - 6,  trackbacks - 0
  1 package  dom;
  2
  3 import  java.io.File;
  4 import  java.io.FileWriter;
  5
  6 import  org.dom4j.Document;
  7 import  org.dom4j.DocumentHelper;
  8 import  org.dom4j.Element;
  9 import  org.dom4j.Node;
 10 import  org.dom4j.io.SAXReader;
 11 import  org.dom4j.io.XMLWriter;
 12
 13 /**
 14  *  @author   ~@_@
 15   */

 16 public   class  Dom4jDemo  {
 17      /**  XML文件路径  */
 18      private  String XMLPath  =   null ;
 19
 20      /**  XML文档  */
 21      private  Document document  =   null ;
 22
 23      public  Dom4jDemo()  {
 24     }

 25
 26      /**
 27      *  初始化xml文件
 28      * 
 29      *  @param  XMLPath 文件路径
 30       */

 31      public  Dom4jDemo(String XMLPath)  {
 32          this .XMLPath  =  XMLPath;
 33     }

 34
 35      /**  * 打开文档  */
 36      public   void  openXML()  {
 37          try   {
 38             SAXReader reader  =   new  SAXReader();
 39              this .document  =  reader.read( this .XMLPath);
 40             System.out.println( " openXML() successful  " );
 41         }
  catch  (Exception e)  {
 42             System.out.println( " openXML() Exception: "   +  e.getMessage());
 43         }

 44     }

 45
 46      /**
 47      *  创建文档
 48      * 
 49      *  @param  rootName 根节点名称
 50       */

 51      public   void  createXML(String rootName)  {
 52          try   {
 53              this .document  =  DocumentHelper.createDocument();
 54             Element root  =  document.addElement(rootName);
 55             System.out.println( " createXML() successful " );
 56         }
  catch  (Exception e)  {
 57             System.out.println( " createXML() Exception: "   +  e.getMessage());
 58         }

 59     }

 60
 61      /**
 62      *  添加根节点的child
 63      * 
 64      *  @param  nodeName 节点名
 65      *  @param  nodeValue 节点值
 66       */

 67      public   void  addNodeFromRoot(String nodeName, String nodeValue)  {
 68         Element root  =   this .document.getRootElement();
 69         Element level1  =  root.addElement(nodeName);
 70         level1.addText(nodeValue);
 71     }

 72
 73      /**
 74      *  打开文档
 75      * 
 76      *  @param  filePath 文档路径
 77       */

 78      public   void  openXML(String filePath)  {
 79          try   {
 80             SAXReader saxReader  =   new  SAXReader();
 81              this .document  =  saxReader.read(filePath);
 82             System.out.println( " openXML(String filePath) successful  " );
 83         }
  catch  (Exception e)  {
 84             System.out.println( " openXML() Exception: "   +  e.getMessage());
 85         }

 86     }

 87
 88      /**  保存文档  */
 89      public   void  saveXML()  {
 90          try   {
 91             XMLWriter output  =   new  XMLWriter( new  FileWriter( new  File(
 92                      this .XMLPath)));
 93             output.write(document);
 94             output.close();
 95             System.out.println( " saveXML() successful  " );
 96         }
  catch  (Exception e1)  {
 97             System.out.println( " saveXML() Exception: "   +  e1.getMessage());
 98         }

 99     }

100
101      /**
102      *  保存文档
103      * 
104      *  @param  toFilePath 保存路径
105       */

106      public   void  saveXML(String toFilePath)  {
107          try   {
108             XMLWriter output  =   new  XMLWriter( new  FileWriter(
109                      new  File(toFilePath)));
110             output.write(document);
111             output.close();
112         }
  catch  (Exception e1)  {
113             System.out.println( " saveXML() Exception: "   +  e1.getMessage());
114         }

115     }

116
117      /**
118      *  获得某个节点的值
119      * 
120      *  @param  nodeName 节点名称
121       */

122      public  String getElementValue(String nodeName)  {
123          try   {
124             Node node  =  document.selectSingleNode( " // "   +  nodeName);
125              return  node.getText();
126         }
  catch  (Exception e1)  {
127             System.out
128                     .println( " getElementValue() Exception: "   +  e1.getMessage());
129              return   null ;
130         }

131     }

132
133      /**
134      *  获得某个节点的子节点的值
135      * 
136      *  @param  nodeName
137      *  @param  childNodeName
138      *  @return
139       */

140      public  String getElementValue(String nodeName, String childNodeName)  {
141          try   {
142             Node node  =   this .document.selectSingleNode( " // "   +  nodeName  +   " / "
143                      +  childNodeName);
144              return  node.getText();
145         }
  catch  (Exception e1)  {
146             System.out
147                     .println( " getElementValue() Exception: "   +  e1.getMessage());
148              return   null ;
149         }

150     }

151
152      /**
153      *  设置一个节点的text
154      * 
155      *  @param  nodeName 节点名
156      *  @param  nodeValue 节点值
157       */

158      public   void  setElementValue(String nodeName, String nodeValue)  {
159          try   {
160             Node node  =   this .document.selectSingleNode( " // "   +  nodeName);
161             node.setText(nodeValue);
162         }
  catch  (Exception e1)  {
163             System.out
164                     .println( " setElementValue() Exception: "   +  e1.getMessage());
165         }

166     }

167
168      /**
169      *  设置一个节点值
170      * 
171      *  @param  nodeName 父节点名
172      *  @param  childNodeName 节点名
173      *  @param  nodeValue 节点值
174       */

175      public   void  setElementValue(String nodeName, String childNodeName,
176             String nodeValue)  {
177          try   {
178             Node node  =   this .document.selectSingleNode( " // "   +  nodeName  +   " / "
179                      +  childNodeName);
180             node.setText(nodeValue);
181         }
  catch  (Exception e1)  {
182             System.out
183                     .println( " setElementValue() Exception: "   +  e1.getMessage());
184         }

185     }

186 }
posted on 2006-09-20 15:45 Warren.Wu 阅读(185) 评论(0)  编辑  收藏 所属分类: DOM4J

只有注册用户登录后才能发表评论。


网站导航: