W3C home > Mailing lists > Public > xmlschema-dev@w3.org > August 2009

Not able to fetch multiple elements from soap response using xpath

From: sridhar veerappan <sriasarch@gmail.com>
Date: Tue, 4 Aug 2009 15:19:48 +0530
Message-ID: <d33701630908040249g530b634t6b3d8631c7abac54@mail.gmail.com>
To: xmlschema-dev@w3c.org
Cc: priscilla@walmsley.com, saul@syncline.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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:15:14 GMT