Java Reference
In-Depth Information
Handler handler = new Handler();
xmlr.setContentHandler(handler);
xmlr.setDTDHandler(handler);
xmlr.setEntityResolver(handler);
xmlr.setErrorHandler(handler);
xmlr.setProperty("http://xml.org/sax/properties/
lexical-handler", handler);
xmlr.parse(new
InputSource(new
FileRead-
er(args[0])));
}
catch (IOException ioe)
{
System.err.println("IOE: "+ioe);
}
catch (SAXException saxe)
{
System.err.println("SAXE: "+saxe);
}
}
}
SAXDemo
is to be run from the command line. After verifying that one or two
command-line arguments (the name of an XML document optionally followed by
lowercase letter v, which tells
SAXDemo
to create a validating parser) have been spe-
cified,
main()
createsan
XMLReader
instance;conditionallyenablesthe
valida-
tion
featureandenablesthe
namespace-prefixes
feature;instantiatesthecom-
panion
Handler
class;installsthis
Handler
instanceastheparser'scontenthandler,
DTDhandler,entity resolver,anderrorhandler; installs this
Handler
instance asthe
value of the
lexical-handler
property; creates an input source to read the docu-
ment from a file; and parses the document.
Listing 10-11.
Handler
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;