Not able to fetch multiple elements from soap response using xpath

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