RT,使用jdom4进行解析。
/**
*
* 判断xml中是否含有一个指定的节点
* @param str xml主题内容
* @param nodeString 指定的节点
* @return
*/
@SuppressWarnings("rawtypes")
public static boolean isExistNote(String str,String nodeString ) {
if (StringUtils.isBlank(str)) {
return false;
}
try {
byte[] byteArray = str.getBytes("UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(byteArrayInputStream);
List list = doc.selectNodes("//"+nodeString);
// "//"的意思表示在任意层级下发现nodeString,不加"//"只会在根节点"nodeString"
if(list.size()!=0){
return true;
}else{
return false;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return false;
}