其他接口返回值的类型为xml格式,这边需要对返回值做一些操作,那么就需要利用dom4j解析。

public static int getNodeCountByXpath(String xmlDoc,String xpath) {
        logger.info("----------------getNodeCountByXpath:xpath="+xpath+"\r\n"+xmlDoc);
        StringReader read = new StringReader(xmlDoc);
        InputSource source = new InputSource(read);
        SAXReader saxReader=new SAXReader();
        Document document;
        int count = 0;
        try {
            document = saxReader.read(source);
            List<Node> nodeList = document.selectNodes(xpath);
            count = nodeList.size();
        } catch (Exception e) {
            logger.error("getNodeCountByXpath 失败,请检查xpath是否正确:"+xpath);
            e.printStackTrace();
        }
        return count;
    }   

发表评论

电子邮件地址不会被公开。 必填项已用*标注