API - Java Example Application

API Documentation Home

Requirements

In order to run this example, you will need:

  • A system with Java 5 or higher installed
  • A basic understanding of the Java programming language

Files

Download ExampleJava.zip. This example contains the following files:

  • ApiWidgetWindow.java
  • Item.java
  • XmlHandler.java
  • ApiWidget.jar
  • readme.txt

Running the Example

To run the application without compiling any source code, use the provided ApiWidget.jar file. Open a console in the directory where the file is located. Then, run:

java -jar ApiWidget.jar

An application window will appear. Click the "Go" button to run the supplied API transaction. Output will appear in the table.

NOTE: For this example, you must always include the "fs_include_headings" parameter. This parameter requests that column headings be included in the response. Without column headings, a table cannot be created.

The Source Code

This example centers on the SAX XML Parser used to parse the XML response. SAX was chosen primarily because it is simple, well documented, and is contained in the default Java installation. The Java Swing classes are used to create the application's GUI.

ApiWidgetWindow.java (main class)

This class extends JFrame and handles the application's GUI. A JTable is used to display the API data.

Item.java

This class represents data for a single form item. The XmlHandler uses this class to store item data as it is being parsed.

XmlHandler.java

This class extend's SAX's DefaultHandler class to create a parser for the API's XML. The following three methods make up the parser:

  • startElement - Called when an XML tag is opened. The parser get the name of the tag and any attributes it contains.
  • endElement - Called when an XML tag is closed. The parser gets the name of the tag.
  • characters - Called when reading the body of an XML tag. The parser gets the character range that this tag contains.

Each of these methods contain a switch statement to take an action depending on what XML tag is currently being parsed.

How it Works

  1. ApiWidgetWindow creates the GUI
  2. When the "Go" button is clicked, ApiWidgetWindow's get getTableData() is called
  3. getTableData() requests the specified URL and creates an XmlHandler to handle the response
  4. The XmlHandler parses the XML with the help of Item
  5. ApiWidgetWindow's getTableData() calls XmlHandler's getHeadings() and getRows() to get the parsed data
  6. The heading and row data is returned in a DefaultTableModel
  7. The JTable accepts the DefaultTableModel and renders the data

API Documentation Home