Article
Consume XML In JSP
XML will save the world!
…At least, that’s what I was told. But somehow, several years later, most of us are not using eXtensible Markup Language (XML) in our every-day programming.
All the same, from time to time, you may be asked to provide a service that will consume or output XML, so it’s a good idea to get comfortable with the technology.
Two examples probably dominate the experience I’ve had with XML. The first is a share price service that a number of companies provide for businesses that want to display their share price information on their Websites. The second is the outputting of companies' press releases as an XML feed for interested parties.
Here, I’ll outline the techniques we use to consume XML in a JSP environment, and briefly comment on outputting XML in a JSP environment. In order to do that, I’ll explain how XML is handled in JSP, and we can then go on to try it out in code.
The XML Share Document
As part of my job, I often have to provide share price information for companies who wish to add the service to their Websites.
Here’s a typical share information document in XML. The service is usually provided over HTTP -- that is, you can find it simply by pointing a browser at a URL such as http://www.myinvestorinfo.com/sharefeed.jsp?company=XXX
A share price document might look like this:
<shares>
<share>
<code>CSW</code>
<price>502.25</price>
<change>2.25</change>
<percentageChange>0.45</
percentageChange>
<open>500</open>
<high>506.32</high>
<low>499.85</low>
<yearLow>423.45</yearLow>
<yearHigh>586.92</yearHigh>
<volume>12486123</volume>
<date>12.45 12/06/2003</date>
</share>
</shares>
If you subscribe to more than one offering, multiple elements are provided. For now, let’s assume that we only need statistics for a single share, and that we specifically need the Price, Change and Percentage Change figures, together with the date the price was set, for that share.
These statistics represent, in order:
- the current value of each share
- the amount by which this value has changed since the last share value in dollars
- the amount by which this value has changed since the last share value as a percentage
- the date and time this value was set
Usually, there is a high cost involved in presenting to-the-minute share updates, so they are often 20 minutes or more out of date.
Java and SAX
For high volume, time critical processing of XML, the Simple API for XML (SAX) is recommended. This basically means that each element and piece of text is passed, in the order in which it occurs, to a processor or handler that is programmed to respond in a predefined way to the elements in the document.
In this case, we have a potentially high volume of data being presented on the site. Any content is time-critical on the Web, and while people will usually forgive site owners for slight delays in financial data updates, there's no point in unnecessarily testing their patience. Ironically, the expectation of up-to-date information seems to be higher in the instance of free content. As there’s no cost to use free services, users will quickly give up on these facilities in favor of the more current information that's so widely available on the Web.
Another benefit that should not be overlooked is that the amount of memory required for document processing in SAX is quite small. Also, our task is very simple (we merely want to extract the information to show it), so there's no need to process or otherwise manipulate the data.
Chanoch is a Senior Developer for Kiwi Media Productions Ltd, which specialises in corporate communications. Chanoch has previously written in a number of books including Professional Apache Tomcat.