JDOM读取XML配置文件--单例模式
2008-12-15 15:47
337 查看
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.JDOMParseException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class TestJDOM {
private static TestJDOM instance = null;
private TestJDOM(){
}
public static synchronized TestJDOM getInstance() {
if (instance==null)
instance = new TestJDOM();
return instance;
}
public String[] parshString(String str){
if(str!=null&&!str.equals("")){
String[] result = str.split(";");
return result;
}else
return null;
}
public String getProperty(Element ParentElement,String[] xPath,int flag){
String result = "";
int index = xPath.length-flag;
String s = xPath[index];
List parentList=ParentElement.getChildren(xPath[index]);
for (Iterator iter = parentList.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
//List list=element.getChildren(xPath[index]);
if(flag!=1){
flag--;
result = getProperty(element,xPath,flag);
}else{
result=element.getText();
}
}
return result;
}
public String getProperty(String file,String path){
String[] xPath = parshString(path);
//String xmlpath="library.xml";
String result = "";
SAXBuilder builder=new SAXBuilder(false);
try {
Document doc= builder.build(file);
Element element=doc.getRootElement();
result = getProperty(element,xPath,xPath.length);
} catch (JDOMParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
public String getProperty(String path){
String[] xPath = parshString(path);
String xmlpath="library.xml";
String result = "";
SAXBuilder builder=new SAXBuilder(false);
try {
Document doc= builder.build(xmlpath);
Element element=doc.getRootElement();
result = getProperty(element,xPath,xPath.length);
} catch (JDOMParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
}
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.JDOMParseException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class TestJDOM {
private static TestJDOM instance = null;
private TestJDOM(){
}
public static synchronized TestJDOM getInstance() {
if (instance==null)
instance = new TestJDOM();
return instance;
}
public String[] parshString(String str){
if(str!=null&&!str.equals("")){
String[] result = str.split(";");
return result;
}else
return null;
}
public String getProperty(Element ParentElement,String[] xPath,int flag){
String result = "";
int index = xPath.length-flag;
String s = xPath[index];
List parentList=ParentElement.getChildren(xPath[index]);
for (Iterator iter = parentList.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
//List list=element.getChildren(xPath[index]);
if(flag!=1){
flag--;
result = getProperty(element,xPath,flag);
}else{
result=element.getText();
}
}
return result;
}
public String getProperty(String file,String path){
String[] xPath = parshString(path);
//String xmlpath="library.xml";
String result = "";
SAXBuilder builder=new SAXBuilder(false);
try {
Document doc= builder.build(file);
Element element=doc.getRootElement();
result = getProperty(element,xPath,xPath.length);
} catch (JDOMParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
public String getProperty(String path){
String[] xPath = parshString(path);
String xmlpath="library.xml";
String result = "";
SAXBuilder builder=new SAXBuilder(false);
try {
Document doc= builder.build(xmlpath);
Element element=doc.getRootElement();
result = getProperty(element,xPath,xPath.length);
} catch (JDOMParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
}
相关文章推荐
- 采用单例模式读取xml配置文件xml
- 在Java中使用Jdom读取xml配置文件
- 【XML配置文件读取】使用jdom读取XML配置文件信息
- C# 单例模式读取xml配置文件属性值
- 利用java反射读取xml配置文件
- Spring MVC 使用 applicationContext.xml 读取项目外 配置文件 配置连接池
- 全局变量的单例模式 从指定的配置文件中读取配置信息
- cocos2dx C++使用rapidxml读取XML配置文件
- java读取配置文件xml
- java读取xml配置文件
- 设计模式之4.2 Spring bean容器以及怎么从xml当中读取配置信息
- c#读取xml配置文件
- jdom学习:读取xml文件
- 怎样获得(读取)web.xml配置文件的参数
- Spring框架中如何读取Spring的XML配置文件
- [Java 15 反射机制 ] 工厂模式与 properties 读取配置文件
- 在wp7中读取XML的配置文件,Content与Resource的区别
- XML配置文件的读取处理
- 使用Linq to Xml 读取配置文件
- JAVA中读取XML文件中的配置信息