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 4213 2008-08-06 00:50:14Z daigle $'
11
  * 
12
  * October Meeting SDSC, 2000
13
-->
14
<HTML>
15
<HEAD>
16
<TITLE>Metacat</TITLE>
17
<link rel="stylesheet" type="text/css" href="./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
      // Get the class name of SAX driver from some XML parser like:
47
      // org.apache.xerces.parsers.SAXParser
48
      <span class="emphasis">String parserName = util.getOption("xml.saxparser");</span>
49
      // Given a class name, this method attempts to load and instantiate the class as an XML reader
50
      <span class="emphasis">parser = XMLReaderFactory.createXMLReader(parserName);</span>
51

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

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

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

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

    
(49-49/58)