使用dom4j和jdom读取xml文件
2010-07-17 14:34
435 查看
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代码
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));
19. Element rootElement=doc.getRootElement();
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. }
依赖于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代码
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));
19. Element rootElement=doc.getRootElement();
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. }
相关文章推荐
- 使用dom4j读取xml配置文件
- 读取xml学习:使用jdom读取xml文件
- dom4j中使用SAXReader读取xml文件出现Premature end of file异常问题
- 在Java中使用Jdom读取xml配置文件
- 使用dom4j读取xml文件
- 使用DOM4J读取XML文件
- jdom或dom4j读取xml文件时如何让dtd验证使用本地dtd文件或者不生效
- 使用Dom4j读取和写入xml文件
- jdom 或 dom4j读取xml文件时如何让dtd验证使用本地dtd文件或者不生效
- 【XML配置文件读取】使用jdom读取XML配置文件信息
- jdom 或 dom4j读取xml文件时如何让dtd验证使用本地dtd文件或者不生效
- 使用JDOM来读取XML文件
- 使用 JDOM 读取和写入 XML 文件
- jdom 或 dom4j读取xml文件时如何让dtd验证使用本地dtd文件或者不生效
- 使用DOM4j读取xml文件
- jdom 或 dom4j读取xml文件时如何让dtd验证使用本地dtd文件或者不生效
- 使用dom4j读取xml文件初始化数据
- 使用dom4j 读取XML文件
- Dom4j SAXReader读取xml异常时占用文件,导致不能移动文件
- dom4j处理xml文件,读取xml字符串,格式化xml文件