Paginating Records Returned By SOAP-based Web Services
Whenever you invoke a web service to retrieve a list of records, the returned list may be long and contain more records than you would want to display. What is called for is usually a pagination mechanism to control which chunk of the returned records to display.
Many front-end UI components such as Data Grids and Pagination controls exist to allow the user to page forwards, backwards or jump to specific pages within a long list. Server-side support for pagination however is not as prevalent and often painstaking to implement, especially in the context of XML records returned by a web service.
The following is a server-side pagination framework for SOAP responses, built in Java.
Download the code from: https://sourceforge.net/projects/soappaginator/files/
The framework transforms a SOAP response into a paged-list of records and returns a requested page to the client. For example, if we have a SOAP response which returns a list of employee records such as:
<Soap:Envelope>
<Soap:Body>
<GetEmployeeResponse xmlns="http://www.aWebService.com/services">
<employees>
<employee>
<employeeid>1234</employeeid>
<name>John Smith</name>
</employee>
<employee>
<employeeid>1235</employeeid>
<name>Joe Turner</name>
</employee>
....
<employee>
<employeeid>1250</employeeid>
<name>Kim Jones</name>
</employee>
</employees>
</GetEmployeeResponse>
</Soap:Body>
</Soap:Envelope>
Let's say that 250 employee records are returned in the above SOAP response.
When the response is submitted to the pagination framework with a specified page size of 10 and page number of 1, the following is returned by the framework:
<PaginatorResult>
<PageNumber>1</PageNumber>
<PageSize>10</PageSize>
<RecordsReturned>10</RecordsReturned>
<TotalRecords>250</TotalRecords>
<TotalPages>25</TotalPages>
<employees>
<employee>
<employeeid>1234</employeeid>
<name>John Smith</name>
</employee>
<employee>
<employeeid>1235</employeeid>
<name>Joe Turner</name>
</employee>
....
<employee>
<employeeid>1244</employeeid>
<name>Jim Howard</name>
</employee>
</employees>
</PaginatorResult>
The result returned is in an easily-consumable format by UI components.
Using the SOAP Response Pagination framework
The framework is simple to use. It consists of one class, SoapResponsePaginator and one method, paginate.
public class SoapResponsePaginator {
public String paginate (String soapResponse, String namespace, String xpathExpression,
int pageNumber, pageSize) {
.......
}
}
The paginate method takes 5 parameters:
- soapResponse - returned by the web service
- namespace - the namespace of the web service
- xpathExpression - the XPath for extracting the data record from the soapResponse
- pageNumber - the page number
- pageSize - the number of records per page
In the example above, you make the following calls:
String soapResponse = ..... // Invoke the web service using JAXP, Axis or a SOAP toolkit
SoapResponsePaginator paginator = new SOAPResponsePaginator();
String paginatedResult = paginator.paginate(soapResponse, "http://www.aWebService.com/services",
"//employees/employee", 1, 10);
Get the code from: https://sourceforge.net/projects/soappaginator/files/