`

dom4j 和jdom的使用用法及jar包下载

    博客分类:
  • java
阅读更多
提供dom4j.jar、jdom.jar、jaxen-1.1.1.jar的下载,在最下面
dom4j读取xml文件:

依赖于jar包: dom4j.jar 和 jaxen-1.1.1.jar
Xml代码 
1. <?xml version="1.0" encoding="UTF-8"?> 
2. <list> 
3. <index>D:\\index\\IndexDB</index> 
4. </list> 

Java代码 
1. package com.wlh.dom4j.test;  
2.  
3. import org.dom4j.Document;  
4. import org.dom4j.DocumentException;  
5. import org.dom4j.Node;  
6. import org.dom4j.io.SAXReader;  
7.  
8. public class TestReader {  
9.     public static void main(String args[]){  
10.         String filePath="index.xml";  
11.         try {  
12.             //如果配置文件是在Src下,则采用如下方式得到Document  
13.             Document document=new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath));  
14.             //如果配置文件是在本地文件系统,则采用如下方式得到Document        
15.             //Document document=new SAXReader().read("D:\\index.xml");  
16.             if (document == null) {     
17.                 System.out.println(filePath+"没找到");     
18.             }else{    
19.             Node node=document.selectSingleNode("//list/index");  
20.             String indexfile=node.getText();  
21.             System.out.println(indexfile);  
22.            }  
23.             } catch (DocumentException e) {  
24.             e.printStackTrace();  
25.               
26.         }     
27.     }  
28. } 




jdom读取xml文件:

依赖于jar包: jdom.jar
得到URL的方式:Thread.currentThread().getContextClassLoader().getResource(filePath)
Java代码 
1. package com.wlh.dom4j.test;  
2.  
3. import java.io.IOException;  
4. import java.util.List;  
5.  
6. import org.jdom.Document;  
7. import org.jdom.Element;  
8. import org.jdom.JDOMException;  
9. import org.jdom.input.SAXBuilder;  
10.  
11. public class TestJdomReader {  
12.     public static void main(String args[]) {  
13.         String filePath = "zxt_index.xml";  
14.         String indexPath="";  
15.         SAXBuilder builder = new SAXBuilder(false);  
16.         try {  
17.             Document doc = builder.build(Thread.currentThread().getContextClassLoader().getResource(filePath));  
18.             Element books = doc.getRootElement();  
19.              Element rootElement= books.getChild("list");  
20.              Element index=rootElement.getChild("index");  
21.              indexPath=index.getText();  
22.              System.out.println(indexPath);  
23.         } catch (JDOMException e) {  
24.             e.printStackTrace();  
25.         } catch (IOException e) {  
26.             e.printStackTrace();  
27.         }  
28.  
29.     }  
30. } 
  • jdom.jar (149.7 KB)
  • 下载次数: 3839
分享到:
评论
9 楼 kingrlf 2015-07-27  
  可以啊,小伙子
8 楼 a574258039 2014-11-03  
7 楼 lantianroom 2014-07-27  
好东西
6 楼 文昌平蓝杰 2013-07-23  
好东西,正需要
5 楼 yao7397 2012-12-12  
   好东西,谢了
4 楼 zhangzhimin2008 2012-07-05  
jdom是什么版本的
3 楼 notOnlyRush 2011-12-19  
好东西,支持一下
2 楼 lsw521314 2011-10-20  
1 楼 eric_shi 2011-04-05  
好东西 正需要

相关推荐

Global site tag (gtag.js) - Google Analytics