Java Reference
In-Depth Information
Although this is hardly a complete definition of XML, which is well beyond the scope of this
topic, it is complete enough for us to see in this chapter how XML and servlets can be used
XML and Java
Now that you understand XML basics, let's take a look at how we can use XML and Java
together. Many Java parsers have been developed to interact with XML documents. The three
most common have been developed by Sun Microsystems, IBM, and Microsoft. For our exam-
ple, you will use Sun's Java API for XML Processing (JAXP), which can be downloaded from
the following URL:
Follow the installation instructions for your platform, including adding the jaxp.jar and the
parser.jar files to your classpath.
Sun's API is composed of two core components, the Document Object Model (DOM) and the
Simple API for XML (SAX API). The DOM is a tree-based API, and the SAX is an event-
based API. For our examples, you will use the SAX API.
Using the SAX API
As we stated earlier, the SAX API is an event-based API. This means that, as the parser parses
the XML document, it triggers certain events based upon encountered elements of the docu-
ment. To see exactly how this works, let's take a look at Listing 10.2.
import java.util.Hashtable;
import java.util.Enumeration;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
public class XMLTest {
public static void main (String argv []) throws IOException {
// Check for the appropriate usage
if ( argv.length != 1 ) {
Search WWH ::

Custom Search