Tutorials : Deliver On-the-Fly Mapping Services to Your Rich Desktop Java Application, Part 2 :

XML Data Implementation

One of the advantages of starting with an XSD is that many programs, such as Liquid XML Studio, can generate a sample XML document directly from an XSD. Additionally, parsing an XML document that is known as valid, according to an XSD, is a simple task. Listing 2 shows a completed XML implementation that you'll use to obtain tiles from the Open Street Map servers.

At the time of writing this article, the BuildURLFunction element in Listing 2 uses JavaScript which produces identical tile requests to that of the OpenStreetMap.org web site's JavaScript. Placing the logic for tile requests in JavaScript is easy and advantageous when the tile server's JavaScript is well known and understood.

Rather than providing a full example, which demonstrates all the additional metadata features, readers may refer to other XML examples here. The next section will show how to parse the XML.

Parsing Your XML with SAX

Parsing XML in Java is straightforward. When XML can be validated against an XSD, as in this case, then Java XML Binding frameworks such as Apache XMLBeans, or JAXB can make the parsing task easy. If data complexity is not an issue, SAX can also be fairly easy to work with and may be preferred over employing the additional overhead of a Java XML Binding framework. In this case, your XSD is not complex—actually, only a few lines long—so SAX is a good light-weight fit for your XML data.

Parsing XML using SAX requires that you extend the appropriate handler class. You'll extend org.xml.sax.helpers.DefaultHandler in a private inner class of the parser class. The handler simply receives notification of what is happening as the XML is being parsed. The code shown in Listing 3 has been simplified to highlight the parsing process, but click here to view the entire code.

The steps of the parsing algorithm, shown below, are very simple and can be modified for parsing most XML data, granted that it is simple in nature.

  1. The startDocument method performs initialization that is done once.
  2. The startElement method checks for the FactoryInfo element and initializes for this element. This method also initializes the current string buffer.
  3. The characters method captures the text for the current element being processed.
  4. The endElement method copies the current string buffer into a data member of the enclosing parser class. This method also builds a new FactoryInfo structure when processing a FactoryInfo element end tag.
Now, you can parse your XML, which then tells you how to access a tile server.

Home / Articles / Deliver On-the-Fly Mapping Services to Your Rich Desktop Java Application, Part 2 / 1 / 2 / Next Page

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.