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


你可能感兴趣的:(DocumentBuilder 解析出指定节点XMLString数据)