- From: sridhar veerappan <sriasarch@gmail.com>
- Date: Tue, 4 Aug 2009 15:19:48 +0530
- To: xmlschema-dev@w3c.org
- Cc: priscilla@walmsley.com, saul@syncline.com
- Message-ID: <d33701630908040249g530b634t6b3d8631c7abac54@mail.gmail.com>
Hi, I am new to xpath, I am using xpath to get the data dynamically from soap response. Soap Response: <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <ns1:getClosestPaymentCenterResponse xmlns:ns1="http://test.com"> <ns1:getClosestPaymentCenterResult> <ns1:Code>1</ns1:Code> <ns1:Message>test</ns1:Message> </ns1:getClosestPaymentCenterResult> <ns1:paymentCenter> <ns1:ID>111</ns1:ID> <ns1:Name>Sri</ns1:Name> <ns1:Address>SUITE</ns1:Address> <ns1:City>BLORE</ns1:City> </ns1:paymentCenter> </ns1:getClosestPaymentCenterResponse> </soapenv:Body> Java Code: public void xpathTest(String xmlFile) throws Exception { XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile("//*[name()='ns1:ID']/text()"); Object result = expr.evaluate(getDocumentFromFile(xmlFile), XPathConstants.STRING); System.out.println("ID:"+result); } public Document getDocumentFromFile(String fName) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); //dbFactory.setNamespaceAware(true); // never forget this! DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); return dBuilder.parse(new File(fName)); } public static void main(String[] args) throws Exception { XPathTest test = new XPathTest(); test.xpathTest("D:/transform/ClosestPaymentCenter.xml"); } I need to get only Code, Address and City.How to get the data using xpath? Thanks in Advance Sri
Received on Wednesday, 5 August 2009 11:43:37 UTC