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

dom4j检查节点问题
Element e1 = (Element) xmldoc.selectSingleNode("//Ly");
if (e1.attribute(abc != null) {
       return e1.attributeValue(abc);
}
return null;

刚开始是这样的,节点Ly存在,也可以查询到,我想知道Ly节点下有没有abc这个属性,如果有就返回abc属性的值,可是如果没有abc这个属性的话,就会报错,应该是e1.attribute(abc != null)这句不是这么写的,搞了半天还没搞明白,后面换一种方式
List list = xmldoc.selectNodes("//Ly[@abc]");
if(list!=null){
     Element node = (Element) list.get(0);
     node.attributeValue(abc);
}
还是不行,abc不存在,list也不为空..
不知道怎么写了,请大家帮忙..谢谢

------解决方案--------------------
你这if也太奇怪了吧:

if (e1.attribute(abc) != null) {
       return e1.attributeValue(abc);
}