Project

General

Profile

1
<!--
2
  * saxparser.html
3
  *
4
  *      Authors: Jivka Bojilova
5
  *    Copyright: 2000 Regents of the University of California and the
6
  *               National Center for Ecological Analysis and Synthesis
7
  *  For Details: http://www.nceas.ucsb.edu/
8
  *      Created: 2000 April 5
9
  *      Version: 0.01
10
  *    File Info: '$Id: saxparser.html 2310 2004-09-21 09:01:22Z jones $'
11
  * 
12
  * October Meeting SDSC, 2000
13
-->
14
<HTML>
15
<HEAD>
16
<TITLE>Metacat</TITLE>
17
<link rel="stylesheet" type="text/css" href="@docrooturl@default.css">
18
</HEAD> 
19
<BODY>
20
  <table width="100%">
21
    <tr>
22
      <td class="tablehead" colspan="2"><p class="label">XML Parsing with SAX</p></td>
23
      <td class="tablehead" colspan="2" align="right">
24
        <a href="./metacatgetlog.html">Back</a> | <a href="./metacattour.html">Home</a> | 
25
        <a href="./xmlindex.html">Next</a>
26
      </td>
27
    </tr>
28
  </table>
29
  <P>SAX, the <i>Simple API for XML</i>, is 
30
  a standard interface for event-based XML parsing. The SAX API provides implemented 
31
  interfaces for many different XML parsers. Metacat requires a SAX2 
32
  API/Java1.2 with any SAX2-compatible XML parser.
33
  <P>To avoid tying Metacat to any specific SAX parser, XMLReaderFactory is used
34
  as the SAX interface to Metacat for easy plugability of parsers. The SAX parser
35
  used must be set in the <a href="./properties.html">Metacat Properties</a> file.</p>
36
  <p>The following is an example of instatiating a parser for Metacat.</p>
37
  <PRE>
38
  XMLReader parser = null;
39
  // Set up the SAX document handlers for parsing
40
  try {
41
      ContentHandler chandler   = new DBSAXHandler(conn, action, docid, user);
42
      EntityResolver dbresolver = new DBEntityResolver(conn, (DBSAXHandler)chandler);
43
      DTDHandler dtdhandler     = new DBDTDHandler(conn);
44

    
45
      // Get an instance of the parser
46
      MetacatUtil util = new MetacatUtil();
47
      // Get the class name of SAX driver from some XML parser like:
48
      // org.apache.xerces.parsers.SAXParser
49
      <span class="emphasis">String parserName = util.getOption("saxparser");</span>
50
      // Given a class name, this method attempts to load and instantiate the class as an XML reader
51
      <span class="emphasis">parser = XMLReaderFactory.createXMLReader(parserName);</span>
52

    
53
      // Turn off validation
54
      parser.setFeature("http://xml.org/sax/features/validation", false);
55
      
56
      parser.setProperty("http://xml.org/sax/properties/declaration-handler", chandler);
57
      parser.setProperty("http://xml.org/sax/properties/lexical-handler", chandler);
58

    
59
      // Set Handlers in the parser to receive XML parsing events
60
      parser.setContentHandler((ContentHandler)chandler);
61
      parser.setEntityResolver((EntityResolver)dbresolver);
62
      parser.setDTDHandler((DTDHandler)dtdhandler);
63
      parser.setErrorHandler((ErrorHandler)chandler);
64

    
65
  } catch (SAXException saxe) {
66
      throw saxe;
67
  }
68

    
69
  </PRE>
70
  
71
  <br>
72
  <a href="./metacatgetlog.html">Back</a> | <a href="./metacattour.html">Home</a> | 
73
  <a href="./xmlindex.html">Next</a>
74
  
75
</BODY>
76
</HTML>
77

    
(44-44/51)