| 发表于:2007-01-10 12:58:361楼 得分:20 |
给楼主一个我写的例子,希望有所帮助: import java.io.file; import java.util.iterator; import java.util.list; import org.dom4j.document; import org.dom4j.documentexception; import org.dom4j.element; import org.dom4j.io.saxreader; public class xmlread { private static string xmlurl; /** * 初始化 * */ public xmlread() { string path = getclass().getprotectiondomain().getcodesource() .getlocation().getpath(); if (path.indexof( "web-inf ") > 0) { path = path.substring(0, path.indexof( "/web-inf/ ") + 9); } this.xmlurl = path; } /** * 根据参数名获得对应参数的值 * @param name * @return */ public static string getxmlvaluebyname(string name) { xmlread xmlread=new xmlread(); string value = null; if (name != null) { saxreader reader = new saxreader(); try { document document = reader.read(new file(xmlread.xmlurl + "/ttms-config.xml ")); list list = document.selectnodes( "/root/element ");// 读取配置文件 iterator iter = list.iterator(); while (iter.hasnext()) { element element = (element) iter.next(); string elementname = element.elementtext( "name "); if (elementname.equals(name)) { value = element.elementtext( "value "); break; } } } catch (documentexception e) { e.printstacktrace(); return null; } } return value; } } | | |
|