其他分享
首页 > 其他分享> > DocumentBuilder 解析出指定节点XMLString数据

DocumentBuilder 解析出指定节点XMLString数据

作者:互联网

/**
     * 获取指定的节点key
     * @param xmlString
     * @param elementKey
     * @return
     */
    public static String parseXMLStringTOGetElement(String xmlString,String elementKey){
        DocumentBuilder builder = XMLHelper.getDocumentBuilder();
        try {
            Document document = builder.parse(new InputSource(new StringReader(xmlString)));
            NodeList nodeList = document.getElementsByTagName(elementKey);
            if(nodeList!=null && nodeList.getLength()>0){
                return nodeList.item(0).getFirstChild().getNodeValue();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取DocumentBuilder对象
     * @return
     */
    public static DocumentBuilder getDocumentBuilder(){
        try {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        }
    }



标签:nodeList,xmlString,elementKey,String,return,DocumentBuilder,节点,XMLString
来源: https://blog.51cto.com/binghe001/2927173