public class MCommonUtil { static boolean result = false; /** * 获取xml结果 * @param protocolXML-xml字符串 * @param findtxt-要查询的字符串 * @return */ public static boolean parse(String protocolXML,String findtxt){ boolean results = false; try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(protocolXML))); Element root = doc.getDocumentElement(); NodeList nodes = root.getChildNodes(); results = foreach(nodes,findtxt); result = false; }catch(Exception e){ e.printStackTrace(); } return results; } /** * 递归处理遍历xml * @param nodelist * @param findtxt * @return */ public static boolean foreach(NodeList nodelist,String findtxt){ if(nodelist!=null){ for(int i=0;i<nodelist.getLength();i++){ Node node = nodelist.item(i); if(node.getFirstChild()!=null){ if(findtxt.equals(node.getFirstChild().getNodeValue())){ result = true; break; } } if(result == false){ foreach(node.getChildNodes(),findtxt); } } } return result; } }