Topic
  • 1 reply
  • Latest Post - ‏2009-09-20T09:57:28Z by SystemAdmin
SystemAdmin
SystemAdmin
746 Posts

Pinned topic xpath and soap response- retrieving multiple elements

‏2009-08-03T10:00:09Z |
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("//*/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 Message, Address and City.How to get the data using xpath?

Thanks in Advance
Sri
Updated on 2009-09-20T09:57:28Z at 2009-09-20T09:57:28Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: xpath and soap response- retrieving multiple elements

    ‏2009-09-20T09:57:28Z  
    [0] Two of the dw articles would set you going:
    ref http://www.ibm.com/developerworks/library/x-javaxpathapi.html
    ref http://www.ibm.com/developerworks/java/library/x-nmspccontext/

    [1] Concretely, you devise, first and above all, a specific NamespaceContext provider for this use. Like this. (Crude, but you can beef it up. I draw reference to the articles mention: I nearly practically invent nothing except concrete expressions.)
    
    
    
    import java.util.Iterator; 
    
    import javax.xml.*; 
    
    import javax.xml.namespace.NamespaceContext; 
    
    class NamespaceContextProvider 
    
    implements NamespaceContext 
    { 
    
    public String getNamespaceURI (String prefix) 
    { 
    
    if (prefix==
    
    null) 
    { 
    
    throw 
    
    new NullPointerException(
    "Null prefix"); 
    } 
    
    else 
    
    if (
    "soapenv".equals(prefix)) 
    { 
    
    return 
    "http://schemas.xmlsoap.org/soap/envelope/"; 
    } 
    
    else 
    
    if (
    "ns1".equals(prefix)) 
    { 
    
    return 
    "http://test.com"; 
    } 
    
    else 
    { 
    
    return XMLConstants.NULL_NS_URI; 
    } 
    }   
    //this method isn't necessary for XPath processing 
    
    public String getPrefix (String uri) 
    { 
    
    throw 
    
    new UnsupportedOperationException(); 
    }   
    //this method isn't necessary for XPath processing 
    
    public Iterator getPrefixes (String uri) 
    { 
    
    throw 
    
    new UnsupportedOperationException(); 
    } 
    }
    

    [2] Then modify the xpathTest method a bit for that.
    
    
    
    public 
    
    void xpathTest(String xmlFile) 
    
    throws Exception 
    { XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(
    
    new NamespaceContextProvider());   
    //*XPathExpression expr = xpath.compile("//*/text()");* String sxpath=
    "/soapenv:Body/ns1:getClosestPaymentCenterResponse/ns1:paymentCenter/ns1:ID"; XPathExpression expr = xpath.compile(sxpath); Object result = expr.evaluate(getDocumentFromFile(xmlFile), XPathConstants.STRING); System.out.println(
    "ID:"+result); 
    }
    

    [2.1] Sure uncomment the line in the getDocumentFromFile method.
    
    dbFactory.setNamespaceAware(
    
    true);
    


    [3] With the above, it should be ready to go with to get the ns1:ID data.