1 package org.xml.sax.demo;
2 // SAX demonstration for parsing from a UTF-16 character stream.
3 // No warranty; no copyright -- use this as you will.
4 // $Id: CharacterStreamDemo.java,v 1.3 1998/05/01 20:44:33 david Exp $
6 import org.xml.sax.Parser;
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
10 import org.xml.sax.helpers.ParserFactory;
12 import java.io.StringReader;
16 * Demonstrate parsing from a UTF-16 character stream.
18 * <p>Usage: java -Dorg.xml.sax.parser=<var>CLASSNAME</var>
19 * CharacterStreamDemo</p>
21 * <p>The SAX parser will read the document from a character
22 * stream connected to an internal string.</p>
24 * <p>Note that the Java implementation of SAX represents a
25 * character stream using a Reader.</p>
28 * @see org.xml.org.xml.sax.parser
29 * @see org.xml.sax.InputSource
30 * @see org.xml.sax.helpers.ParserFactory
31 * @see java.io.StringReader
33 public class CharacterStreamDemo {
35 // This is the document, in all its glory.
36 // In a read-world application, this
37 // could come from a database, a text
38 // field, or just about anywhere.
39 final static String doc = "<?xml version=\"1.0\"?>" +
41 "<title>Hello</title>\n" +
42 "<para>Hello, world!</para>\n" +
46 * Main entry point for an application.
48 public static void main (String args[])
56 // First, check the command-line
58 if (args.length != 0) {
59 System.err.println("Usage: java CharTest");
63 // Allocate a SAX Parser object, using
64 // the class name provided in the
65 // org.xml.sax.parser property (you could
66 // also specify a classname here).
67 parser = ParserFactory.makeParser();
69 // Allocate an event handler, and register
70 // it with the SAX parser for all four
71 // types of events (we could use a
72 // separate object for each).
73 handler = new DemoHandler();
74 parser.setEntityResolver(handler);
75 parser.setDTDHandler(handler);
76 parser.setDocumentHandler(handler);
77 parser.setErrorHandler(handler);
79 // Create a Java-specific StringReader
80 // for the internal document.
81 reader = new StringReader(doc);
83 // Parse the document from the
85 parser.parse(new InputSource(reader));