我就想在XML里设置变量,然后在类里面获取到。方便以后项目做更改。谁能给个最简单的方法。 最好把设置变量和获取的过程都写出来。呵呵~
解决方案 »
- 关于Hibernate HQL IN()操作符问题!
- MySQL一个让人郁闷的问题……请高手指教,(分不多,望成全)在线等!!
- 学EJB,还是学Struts+Spring+Hibernate
- 大家推荐本书给我吧
- 高分求Spring中的getJdbcTemplate().queryForList()使用方法
- 在struts中,该如何遍历这样的Bean???
- 初学struts 一个简单的问题,请各位帮助!在线等待
- j2ee 工程之间实体bean的操作
- 后台判断是否需要显示加载更多按钮
- An error occured trying to find to javax.faces.webapp.FacesServlet
- 通用DAO开发框架问题!
- 菜鸟问题,端口号
网上搜一下比较多的
http://lavasoft.blog.51cto.com/62575/66953
Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath));
List modules = document.selectNodes("//Modules/Module;
for (Iterator iter = modules.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
......
}
记不清了
<param-name>page_size</param-name>
<param-value>5</param-value>
</context-param> <context-param>
<param-name>max_index_pages</param-name>
<param-value>5</param-value>
</context-param>那么我想在类中读取到这两个值,该如何读取~
import java.io.File;
import java.util.List;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;public class Dom4jApp {
public static Document parse(String fileName) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
return document;
}
public static void main(String[] args) throws DocumentException {
String fileName = "resources/a.xml";
Document doc = parse(fileName);
List nodes = doc.selectNodes( "/root/context-param/param-name" );
for (Object obj : nodes) {
Node n = (Node) obj;
System.out.printf("Tag: %s, Text: %s\n", n.getName(), n.getText());
}
nodes = doc.selectNodes( "/root/context-param/param-value" );
for (Object obj : nodes) {
Node n = (Node) obj;
System.out.printf("Tag: %s, Text: %s\n", n.getName(), n.getText());
}
}
}
谢了~不过这样还是太麻烦了,还是不用dom4j了吧,肯定还有其他方法可以轻松获取里面的值的!
以前写过的,楼主可以参考一下。
获取key=value文件的值
resourcebundle.getBundle()
这个能获取xml文件吗? 那文件的路径要怎么获取呢? 我获取不到,我的xml文件路径在:
/【项目名】/WebRoot/WEB-INF/web.xml这么写没办法获取啊,应该怎么写?
name=刘友
sex=男