原因分析:xml文件中含有xmlns命名空间属性。
解决方案:
xml文件
<web-app xmlns="http://java.sun.com/xml/ns/javaee">
<display-name>a</display-name>
<welcome-file-list>
<welcome-file>b</welcome-file>
</welcome-file-list>
</web-app>
java代码
public class TransferXML {
public static void main(String[] args) throws Exception{
SAXReader saxReader = new SAXReader();
File file = new File("D:\test.xml");
Document document = saxReader.read(file);
Map map = new HashMap();
map.put("xmlns","http://java.sun.com/xml/ns/javaee");
XPath x = document.createXPath("//xmlns:welcome-file");
x.setNamespaceURIs(map);
List nodelist = x.selectNodes(document);
System.out.println(nodelist.size());
}
}