Just a method to extract Sub - XML from XML using XPath in Java
 public String extractResponseData(String responseXML) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, TransformerException {
        
        XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//responsedata/*");
        InputSource inputSource = new InputSource(new  ByteArrayInputStream(responseXML.getBytes()));
        NodeList nodeList = (NodeList) expr.evaluate(inputSource, XPathConstants.NODESET);
        StringWriter sw = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(nodeList.item(0)), new StreamResult(sw));
        return sw.toString();
    }
No comments:
Post a Comment