dom4j selectSingleNode方法返回为null

kingwang2012 2007-07-23 02:37:10
下面一段代码用于取出web.xml中的session-timeout元素的值,我调用了dom4j的selectSingleNode方法,但是返回的值总是null,请大家帮我看看错在哪里?
public HashMap getSessionTimeOut(String fileName)throws MalformedURLException,
DocumentException
{
HashMap map=new HashMap();

try
{
Document document = read(fileName);

Node node=document.selectSingleNode("//session-timeout");

map.put("session-timeout", node.getText());
}
catch (DocumentException e)
{
System.out.println(e.getMessage());
}
catch (IOException es)
{
System.out.println(es.getMessage());
}
catch(Exception ee)
{
ee.printStackTrace();
}
return map;
}
document 的值肯定不为null,这个我已经跟踪过。我再web.xml中设置了这个元素:
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>30</session-timeout>
</session-config>
每次总说node为null,不知道为什么
...全文
2079 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
guguai 2012-09-08
  • 打赏
  • 举报
回复
楼主可以考虑检查是否是由于没有考虑XML文件的命名空间的问题,如果有命名空间,则只能查找根节点成功,其余节点都会返回null。 可考虑用如下方法访问:
Document document = read(fileName);
Dom4jXPath path = new Dom4jXPath("//ns:session-timeout");
path.addNamespace("ns", "http://java.sun.com/xml/ns/j2ee");
Element e = (Element) path.selectSingleNode(document);

System.out.println(e.getName()); // check if it is null
追梦船 2011-09-18
  • 打赏
  • 举报
回复
我也遇到相同问题 。。。同求解答
shilei41193 2010-11-09
  • 打赏
  • 举报
回复
C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与xPath 介绍 ...
www.cnblogs.com/shelley/archive/2010/10/.../1862289.html
kingwang2012 2007-07-23
  • 打赏
  • 举报
回复
自己up
kingwang2012 2007-07-23
  • 打赏
  • 举报
回复
我已经写成
Node node=document.selectSingleNode("//web-app/session-config/session-timeout");还是不行
改成Node node=document.selectSingleNode("/web-app/session-config/session-timeout");
和Node node=document.selectSingleNode("/session-config/session-timeout");仍然不行,请各位高手看看为什么啊?
13659242250 2007-07-23
  • 打赏
  • 举报
回复
1,该加的jar包,有没有添加!
2,又没有抛出什么异常?
3,xpath解析的时候,应该加上他的父节点!
loveyt 2007-07-23
  • 打赏
  • 举报
回复
//session-timeout的路径是表示该XML文档中所有的session-timeout节点,有可能不是唯一的.所以用这个做xpath的话不一定返回的是唯一的一个Node,这个是我猜的,不知道对不对.LS的方法是正确的.
ps_ps 2007-07-23
  • 打赏
  • 举报
回复
document.selectSingleNode("//session-timeout");应该加上他的父节点session-config

document.selectSingleNode("/session-config/session-timeout");或者更上一层父节点,可以试试

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧