API - Java Example Application
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
Download ExampleJava.zip. This example contains the following files:
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 represents data for a single form item. The XmlHandler uses this class to store item data as it is being parsed.
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
- ApiWidgetWindow creates the GUI
- When the "Go" button is clicked, ApiWidgetWindow's get getTableData() is called
- getTableData() requests the specified URL and creates an XmlHandler to handle the response
- The XmlHandler parses the XML with the help of Item
- ApiWidgetWindow's getTableData() calls XmlHandler's getHeadings() and getRows() to get the parsed data
- The heading and row data is returned in a DefaultTableModel
- The JTable accepts the DefaultTableModel and renders the data