日期:2014-05-18  浏览次数:21098 次

dom4j selectSingleNode方法返回为null
下面一段代码用于取出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,不知道为什么

------解决方案--------------------
document.selectSingleNode( "//session-timeout ");应该加上他的父节点session-config

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

------解决方案--------------------
//session-timeout的路径是表示该XML文档中所有的session-timeout节点,有可能不是唯一的.所以用这个做xpath的话不一定返回的是唯一的一个Node,这个是我猜的,不知道对不对.LS的方法是正确的.