码迷,mamicode.com
首页 > 其他好文 > 详细

解析xml,判断是否含有某字符串

时间:2017-12-30 12:09:30      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:actor   判断   new   stack   eva   lxml   ack   blog   i++   

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;
    }
}

 

解析xml,判断是否含有某字符串

标签:actor   判断   new   stack   eva   lxml   ack   blog   i++   

原文地址:https://www.cnblogs.com/jassy/p/8148902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!