Jerry Jacob's Blog

Web Service Integration using JXPath

Posted in Java/ J2EE by jerlinworld on August 5, 2009

I was successful in integrating two web services using the JXPath API from Apache. My task was to generate a letter using the data obtained from a third party web service. I had to call another web service to get the letter template and the metadata that’s needed for printing the letter. Since these services were in no way related, I had to create a file; mapping the data items in our web service to the actual data in third party web service (key-value). Using this mapping file along with the JXPath API made the integration a piece of cake.

Now let me explain how the JXPath works. Consider this example:

String city = (String)JXPathContext.newContext(vendor).
         getValue("locations[address/zipCode='90210']/address/city");

This XPath expression is equivalent to the following Java code:

String city = null;
Collection locations = vendor.getLocations();
Iterator it = locations.iterator();
while (it.hasNext()){
    Location location = (Location)it.next();
    String zipCode = location.getAddress().getZipCode();
    if (zipCode.equals("90210")){
      city = location.getAddress().getCity();
      break;
    }
}

In my mapping file, I have my values stored as XPath strings. These XPath values were used to fetch the actual data from the third party web service using the JXPath API.

Advertisements