Revision 55
Added by Matt Jones over 24 years ago
DBSAXWriter.java | ||
---|---|---|
90 | 90 |
* @param filename the filename to be loaded into the database |
91 | 91 |
* @param conn the database connection to which to write the XML file |
92 | 92 |
*/ |
93 |
public DBSAXWriter( Reader xml, Connection conn) |
|
94 |
throws IOException, |
|
95 |
SQLException, |
|
96 |
ClassNotFoundException |
|
97 |
{ |
|
98 |
this.conn = conn; |
|
99 |
|
|
100 |
//try { |
|
101 |
|
|
102 |
try { |
|
103 |
SAXParser parser = initializeParser(conn); |
|
104 |
parser.parse(xml); |
|
105 |
} catch (SAXParseException e) { |
|
106 |
System.err.println(e.getMessage()); |
|
107 |
} catch (SAXException e) { |
|
108 |
System.err.println(e.getMessage()); |
|
109 |
} |
|
110 |
//} catch (Exception e) { |
|
111 |
//System.err.println(e.toString()); |
|
112 |
//} |
|
113 |
} |
|
114 |
|
|
93 | 115 |
public DBSAXWriter( String filename, Connection conn) |
94 | 116 |
throws IOException, |
95 | 117 |
SQLException, |
96 | 118 |
ClassNotFoundException |
97 |
{
|
|
119 |
{ |
|
98 | 120 |
this.conn = conn; |
99 |
|
|
100 |
// |
|
101 |
// Set up the SAX document handlers for parsing |
|
102 |
// |
|
103 |
|
|
121 |
FileReader xmlfile = new FileReader(new File(filename).toString()); |
|
104 | 122 |
try { |
123 |
SAXParser parser = initializeParser(conn); |
|
124 |
parser.parse(xmlfile); |
|
125 |
} catch (SAXParseException e) { |
|
126 |
System.err.println(e.getMessage()); |
|
127 |
} catch (SAXException e) { |
|
128 |
System.err.println(e.getMessage()); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
private SAXParser initializeParser(Connection conn) { |
|
133 |
SAXParser parser = null; |
|
134 |
// |
|
135 |
// Set up the SAX document handlers for parsing |
|
136 |
// |
|
137 |
try { |
|
138 |
// Use the XMLDocumentHandler interface for namespace support |
|
139 |
// instead of org.xml.sax.DocumentHandler |
|
140 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn); |
|
105 | 141 |
|
106 |
// Use the XMLDocumentHandler interface for namespace support
|
|
107 |
// instead of org.xml.sax.DocumentHandler
|
|
108 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
|
|
142 |
// For all the other interface use the default provided by
|
|
143 |
// Handler base
|
|
144 |
HandlerBase defHandler = new HandlerBase();
|
|
109 | 145 |
|
110 |
// For all the other interface use the default provided by |
|
111 |
// Handler base |
|
112 |
HandlerBase defHandler = new HandlerBase(); |
|
146 |
// Get an instance of the parser |
|
147 |
parser = new SAXParser(); |
|
113 | 148 |
|
114 |
// Get an instance of the parser |
|
115 |
SAXParser parser = new SAXParser(); |
|
149 |
// Set Handlers in the parser |
|
150 |
// Set the DocumentHandler to XMLDocumentHandler |
|
151 |
parser.setDocumentHandler(xmlDocHandler); |
|
116 | 152 |
|
117 |
// Set Handlers in the parser |
|
118 |
// Set the DocumentHandler to XMLDocumentHandler |
|
119 |
parser.setDocumentHandler(xmlDocHandler); |
|
153 |
// Set the other Handler to the defHandler |
|
154 |
parser.setErrorHandler(defHandler); |
|
155 |
parser.setEntityResolver(defHandler); |
|
156 |
parser.setDTDHandler(defHandler); |
|
120 | 157 |
|
121 |
// Set the other Handler to the defHandler |
|
122 |
parser.setErrorHandler(defHandler); |
|
123 |
parser.setEntityResolver(defHandler); |
|
124 |
parser.setDTDHandler(defHandler); |
|
158 |
} catch (Exception e) { |
|
159 |
System.err.println(e.toString()); |
|
160 |
} |
|
125 | 161 |
|
126 |
try |
|
127 |
{ |
|
128 |
parser.parse(fileToURL(new File(filename)).toString()); |
|
129 |
} |
|
130 |
catch (SAXParseException e) |
|
131 |
{ |
|
132 |
System.err.println(filename + ": " + e.getMessage()); |
|
133 |
} |
|
134 |
catch (SAXException e) |
|
135 |
{ |
|
136 |
System.err.println(filename + ": " + e.getMessage()); |
|
137 |
} |
|
138 |
} |
|
139 |
catch (Exception e) |
|
140 |
{ |
|
141 |
System.err.println(e.toString()); |
|
142 |
} |
|
162 |
return parser; |
|
163 |
} |
|
143 | 164 |
|
144 |
} |
|
145 |
|
|
146 |
/** Utility method to convert a file handle into a URL */ |
|
147 |
static public URL fileToURL(File file) |
|
148 |
{ |
|
165 |
/** Utility method to convert a file handle into a URL */ |
|
166 |
static public URL fileToURL(File file) |
|
167 |
{ |
|
149 | 168 |
String path = file.getAbsolutePath(); |
150 | 169 |
String fSep = System.getProperty("file.separator"); |
151 | 170 |
if (fSep != null && fSep.length() == 1) |
... | ... | |
161 | 180 |
throw new Error("unexpected MalformedURLException"); |
162 | 181 |
} |
163 | 182 |
} |
164 |
|
|
165 | 183 |
} |
Also available in: Unified diff
added xml doc loading to servlet actions