通过JDom读取XML文件
2007-12-20 11:57
453 查看
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ReadConfig {
private static ReadConfig config = new ReadConfig();
private ReadConfig() {
}
public static ReadConfig getInstance() {
return config;
}
public String getPath() {
String path = this.getClass().getResource("").getPath();
path = path.substring(0, path.indexOf("classes"));
path += "classes/conf.xml";
//解决配置文件路径问题,把%20改成空格
path=path.replaceAll("%20", " ");
// URL url=ReadConfig.class.getClassLoader().getResource("");
// String path=url.getPath()+"/conf.xml";
return path;
}
public Map<String, String> readFromXml() {
Map<String, String> map = new HashMap<String, String>();
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream(getPath()));
Element root = doc.getRootElement();
List root1 = root.getChildren();
List sib=null;
for (Object root2 : root1) {//这里使用增强循环
sib=((Element)root2).getChildren();
//System.out.println(sib);
for(Object elee:sib)
{
Element e=(Element)elee;
map.put(e.getName(), e.getText());
//System.out.println(e.getName()+"="+e.getText());
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return map;
}
}
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ReadConfig {
private static ReadConfig config = new ReadConfig();
private ReadConfig() {
}
public static ReadConfig getInstance() {
return config;
}
public String getPath() {
String path = this.getClass().getResource("").getPath();
path = path.substring(0, path.indexOf("classes"));
path += "classes/conf.xml";
//解决配置文件路径问题,把%20改成空格
path=path.replaceAll("%20", " ");
// URL url=ReadConfig.class.getClassLoader().getResource("");
// String path=url.getPath()+"/conf.xml";
return path;
}
public Map<String, String> readFromXml() {
Map<String, String> map = new HashMap<String, String>();
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream(getPath()));
Element root = doc.getRootElement();
List root1 = root.getChildren();
List sib=null;
for (Object root2 : root1) {//这里使用增强循环
sib=((Element)root2).getChildren();
//System.out.println(sib);
for(Object elee:sib)
{
Element e=(Element)elee;
map.put(e.getName(), e.getText());
//System.out.println(e.getName()+"="+e.getText());
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return map;
}
}
相关文章推荐
- Eclipse中通过FileSystemXmlApplicationContext读取不到配置文件问题
- jdom学习:读取xml文件
- 使用JDOM来读取XML文件
- Jdom读取xml文件
- JDOM 读取xml文件中的CDATA数据
- jdom学习:读取xml文件
- web.xml中通过contextConfigLocation的读取spring的配置文件
- 【java开发系列】— JDOM创建、改动、删除、读取XML文件
- JS通过ajax动态读取xml文件内容的方法
- flex通过HTTPService组件读取复杂结构的xml文件
- [经典案例]Jdom读取xml文件
- 通过java程序读取文件xml中的数据
- 【XML配置文件读取】使用jdom读取XML配置文件信息
- [Java] JDOM 读取 xml 文件 示例程序初步
- 【java开发系列】— JDOM创建、修改、删除、读取XML文件
- JS通过XMLHttpRequest 读取服务器端文件
- jdom学习:读取xml文件
- 在Java中使用Jdom读取xml配置文件
- jdom学习:读取xml文件
- jdom学习:读取xml文件