SAX解析XML文件并插入数据库
dblp.xml
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE dblp SYSTEM "dblp.dtd"> 3 <dblp><article mdate="2017-05-11" key="journals/ijtm/LuMW10"> 4 <author>Iuan-Yuan Lu</author> 5 <author>Chih-Jen Mao</author> 6 <author>Chun-Hsien Wang</author> 7 <title>Intrafirm technology and knowledge transfer: a best practice perspective.</title> 8 <pages>338-356</pages> 9 <year>2010</year> 10 <volume>49</volume> 11 <journal>IJTM</journal> 12 <number>4</number> 13 <ee>https://doi.org/10.1504/IJTM.2010.030162</ee> 14 <url>db/journals/ijtm/ijtm49.html#LuMW10</url> 15 </article><article mdate="2017-05-11" key="journals/ijtm/TingTH14"> 16 <author>Kuo-Chang Ting</author> 17 <author>Ping Ho Ting</author> 18 <author>Po-Wen Hsiao</author> 19 <title>Why are bloggers willing to share their thoughts via travel blogs?</title> 20 <pages>89-108</pages> 21 <year>2014</year> 22 <volume>64</volume> 23 <journal>IJTM</journal> 24 <number>1</number> 25 <ee>https://doi.org/10.1504/IJTM.2014.059237</ee> 26 <url>db/journals/ijtm/ijtm64.html#TingTH14</url> 27 </article> 28 </dblp>
dblp.dtd
1 <!-- 2 DBLP XML Records are available from 3 4 http://dblp.uni-trier.de/xml/ 5 6 DBLP: Copyright 1993-2016 by Schloss Dagstuhl - Leibniz Center for Informatics (German: Schloss Dagstuhl - Leibniz-Zentrum für Informatik GmbH) 7 and Michael Ley (Universität Trier, Informatik, ley@uni-trier.de) and 8 9 The data provided by DBLP on its webpages as well as the XML files available at http://dblp.uni-trier.de/xml/ 10 are released under the Open Data Commons Attribution License (ODC-BY 1.0). You are free to copy, distribute, use, modify, 11 transform, build upon and produce derived works from our data as long as you attribute any public use of the data, or works 12 produced from the data, in the manner specified in the license. Read the full ODC-BY 1.0 license text for the exact terms 13 that apply. The ODC-BY 1.0 license is courtesy of the Open Knowledge Foundation. 14 15 http://opendatacommons.org/licenses/by/1.0/ 16 http://okfn.org/ 17 18 In the eyes of DBLP, a simple note refering to DBLP and/or a link back to http://dblp.uni-trier.de/ is sufficient to meet the 19 attribution criterion. Individual use of only a small number of records (such as a publication list of a few authors or the 20 table of content of a proceedings volume) does not need attribution. Of course, we are always happy if you link to us. 21 22 For further details on the content of attributes see http://dblp.org/faq/What+do+I+find+in+dblp+xml.html 23 24 Date of this File: August 29, 2017 25 26 A changelog for the dblp.dtd can be found at: dblp.org/xml 27 28 --> 29 30 <!ELEMENT dblp (article|inproceedings|proceedings|book|incollection| 31 phdthesis|mastersthesis|www|person|data)*> 32 <!ATTLIST dblp mdate CDATA #IMPLIED > 33 34 <!ENTITY % field "author|editor|title|booktitle|pages|year|address|journal|volume|number|month|url|ee|cdrom|cite|publisher|note|crossref|isbn|series|school|chapter|publnr"> 35 36 <!ELEMENT article (%field;)*> 37 <!ATTLIST article 38 key CDATA #REQUIRED 39 mdate CDATA #IMPLIED 40 publtype CDATA #IMPLIED 41 reviewid CDATA #IMPLIED 42 rating CDATA #IMPLIED 43 cdate CDATA #IMPLIED 44 > 45 46 <!ELEMENT inproceedings (%field;)*> 47 <!ATTLIST inproceedings key CDATA #REQUIRED 48 mdate CDATA #IMPLIED 49 publtype CDATA #IMPLIED 50 cdate CDATA #IMPLIED 51 > 52 53 <!ELEMENT proceedings (%field;)*> 54 <!ATTLIST proceedings key CDATA #REQUIRED 55 mdate CDATA #IMPLIED 56 publtype CDATA #IMPLIED 57 cdate CDATA #IMPLIED 58 > 59 60 <!ELEMENT book (%field;)*> 61 <!ATTLIST book key CDATA #REQUIRED 62 mdate CDATA #IMPLIED 63 publtype CDATA #IMPLIED 64 cdate CDATA #IMPLIED 65 > 66 67 <!ELEMENT incollection (%field;)*> 68 <!ATTLIST incollection key CDATA #REQUIRED 69 mdate CDATA #IMPLIED 70 publtype CDATA #IMPLIED 71 cdate CDATA #IMPLIED 72 > 73 74 <!ELEMENT phdthesis (%field;)*> 75 <!ATTLIST phdthesis key CDATA #REQUIRED 76 mdate CDATA #IMPLIED 77 publtype CDATA #IMPLIED 78 cdate CDATA #IMPLIED 79 > 80 81 <!ELEMENT mastersthesis (%field;)*> 82 <!ATTLIST mastersthesis key CDATA #REQUIRED 83 mdate CDATA #IMPLIED 84 publtype CDATA #IMPLIED 85 cdate CDATA #IMPLIED 86 > 87 88 <!ELEMENT www (%field;)*> 89 <!ATTLIST www key CDATA #REQUIRED 90 mdate CDATA #IMPLIED 91 publtype CDATA #IMPLIED 92 cdate CDATA #IMPLIED 93 > 94 95 <!ELEMENT data (%field;)*> 96 <!ATTLIST data key CDATA #REQUIRED 97 mdate CDATA #IMPLIED 98 publtype CDATA #IMPLIED 99 cdate CDATA #IMPLIED 100 > 101 102 <!ELEMENT person ((author*, (note|url|cite)*)|crossref) > 103 <!ATTLIST person key CDATA #REQUIRED 104 mdate CDATA #IMPLIED 105 cdate CDATA #IMPLIED 106 > 107 108 <!ELEMENT author (#PCDATA)> 109 <!ATTLIST author 110 aux CDATA #IMPLIED 111 bibtex CDATA #IMPLIED 112 orcid CDATA #IMPLIED 113 label CDATA #IMPLIED 114 type CDATA #IMPLIED 115 > 116 117 <!ELEMENT editor (#PCDATA)> 118 <!ATTLIST editor 119 aux CDATA #IMPLIED 120 orcid CDATA #IMPLIED 121 label CDATA #IMPLIED 122 type CDATA #IMPLIED 123 > 124 125 <!ELEMENT address (#PCDATA)> 126 <!ATTLIST address 127 aux CDATA #IMPLIED 128 label CDATA #IMPLIED 129 type CDATA #IMPLIED 130 > 131 132 <!ENTITY % titlecontents "#PCDATA|sub|sup|i|tt|ref"> 133 <!ELEMENT title (%titlecontents;)*> 134 <!ATTLIST title 135 bibtex CDATA #IMPLIED 136 aux CDATA #IMPLIED 137 label CDATA #IMPLIED 138 type CDATA #IMPLIED 139 > 140 141 <!ELEMENT booktitle (#PCDATA)> 142 <!ATTLIST booktitle 143 aux CDATA #IMPLIED 144 label CDATA #IMPLIED 145 type CDATA #IMPLIED 146 > 147 148 <!ELEMENT pages (#PCDATA)> 149 <!ATTLIST pages 150 aux CDATA #IMPLIED 151 label CDATA #IMPLIED 152 type CDATA #IMPLIED 153 > 154 155 <!ELEMENT year (#PCDATA)> 156 <!ATTLIST year 157 aux CDATA #IMPLIED 158 label CDATA #IMPLIED 159 type CDATA #IMPLIED 160 > 161 162 <!ELEMENT journal (#PCDATA)> 163 <!ATTLIST journal 164 aux CDATA #IMPLIED 165 label CDATA #IMPLIED 166 type CDATA #IMPLIED 167 > 168 169 <!ELEMENT volume (#PCDATA)> 170 <!ATTLIST volume 171 aux CDATA #IMPLIED 172 label CDATA #IMPLIED 173 type CDATA #IMPLIED 174 > 175 176 <!ELEMENT number (#PCDATA)> 177 <!ATTLIST number 178 aux CDATA #IMPLIED 179 label CDATA #IMPLIED 180 type CDATA #IMPLIED 181 > 182 183 <!ELEMENT month (#PCDATA)> 184 <!ATTLIST month 185 aux CDATA #IMPLIED 186 label CDATA #IMPLIED 187 type CDATA #IMPLIED 188 > 189 190 <!ELEMENT url (#PCDATA)> 191 <!ATTLIST url 192 aux CDATA #IMPLIED 193 label CDATA #IMPLIED 194 type CDATA #IMPLIED 195 > 196 197 <!ELEMENT ee (#PCDATA)> 198 <!ATTLIST ee 199 aux CDATA #IMPLIED 200 label CDATA #IMPLIED 201 type CDATA #IMPLIED 202 > 203 204 <!ELEMENT cite (#PCDATA)> 205 <!ATTLIST cite 206 aux CDATA #IMPLIED 207 label CDATA #IMPLIED 208 type CDATA #IMPLIED 209 ref CDATA #IMPLIED 210 > 211 212 <!ELEMENT school (#PCDATA)> 213 <!ATTLIST school 214 aux CDATA #IMPLIED 215 label CDATA #IMPLIED 216 type CDATA #IMPLIED 217 > 218 219 <!ELEMENT publisher (#PCDATA)> 220 <!ATTLIST publisher 221 href CDATA #IMPLIED 222 aux CDATA #IMPLIED 223 label CDATA #IMPLIED 224 type CDATA #IMPLIED 225 > 226 227 <!ELEMENT note (#PCDATA)> 228 <!ATTLIST note 229 aux CDATA #IMPLIED 230 label CDATA #IMPLIED 231 type CDATA #IMPLIED 232 > 233 234 <!ELEMENT cdrom (#PCDATA)> 235 236 <!ELEMENT crossref (#PCDATA)> 237 <!ELEMENT isbn (#PCDATA)> 238 <!ATTLIST isbn 239 aux CDATA #IMPLIED 240 label CDATA #IMPLIED 241 type CDATA #IMPLIED 242 > 243 244 <!ELEMENT chapter (#PCDATA)> 245 <!ELEMENT series (#PCDATA)> 246 <!ATTLIST series 247 href CDATA #IMPLIED 248 aux CDATA #IMPLIED 249 label CDATA #IMPLIED 250 type CDATA #IMPLIED 251 > 252 253 <!ELEMENT publnr (#PCDATA) > 254 <!ATTLIST publnr 255 aux CDATA #IMPLIED 256 label CDATA #IMPLIED 257 type CDATA #IMPLIED 258 > 259 260 <!-- sub elements of the title element --> 261 <!ELEMENT ref (#PCDATA)> 262 <!ATTLIST ref href CDATA #REQUIRED> 263 <!ELEMENT sup (%titlecontents;)*> 264 <!ELEMENT sub (%titlecontents;)*> 265 <!ELEMENT i (%titlecontents;)*> 266 <!ELEMENT tt (%titlecontents;)*> 267 268 <!ENTITY reg "®"> 269 <!ENTITY micro "µ"> 270 <!ENTITY times "×"> 271 272 <!-- (C) International Organization for Standardization 1986 273 Permission to copy in any form is granted for use with 274 conforming SGML systems and applications as defined in 275 ISO 8879, provided this notice is included in all copies. 276 --> 277 <!-- Character entity set. Typical invocation: 278 <!ENTITY % HTMLlat1 PUBLIC 279 "ISO 8879-1986//ENTITIES Added Latin 1//EN//XML"> 280 --> 281 <!-- This version of the entity set can be used with any SGML document 282 which uses ISO 8859-1 or ISO 10646 as its document character 283 set. This includes XML documents and ISO HTML documents. 284 --> 285 286 <!ENTITY Agrave "À" ><!-- capital A, grave accent --> 287 <!ENTITY Aacute "Á" ><!-- capital A, acute accent --> 288 <!ENTITY Acirc "Â" ><!-- capital A, circumflex accent --> 289 <!ENTITY Atilde "Ã" ><!-- capital A, tilde --> 290 <!ENTITY Auml "Ä" ><!-- capital A, dieresis or umlaut mark --> 291 <!ENTITY Aring "Å" ><!-- capital A, ring --> 292 <!ENTITY AElig "Æ" ><!-- capital AE diphthong (ligature) --> 293 <!ENTITY Ccedil "Ç" ><!-- capital C, cedilla --> 294 <!ENTITY Egrave "È" ><!-- capital E, grave accent --> 295 <!ENTITY Eacute "É" ><!-- capital E, acute accent --> 296 <!ENTITY Ecirc "Ê" ><!-- capital E, circumflex accent --> 297 <!ENTITY Euml "Ë" ><!-- capital E, dieresis or umlaut mark --> 298 <!ENTITY Igrave "Ì" ><!-- capital I, grave accent --> 299 <!ENTITY Iacute "Í" ><!-- capital I, acute accent --> 300 <!ENTITY Icirc "Î" ><!-- capital I, circumflex accent --> 301 <!ENTITY Iuml "Ï" ><!-- capital I, dieresis or umlaut mark --> 302 <!ENTITY ETH "Ð" ><!-- capital Eth, Icelandic --> 303 <!ENTITY Ntilde "Ñ" ><!-- capital N, tilde --> 304 <!ENTITY Ograve "Ò" ><!-- capital O, grave accent --> 305 <!ENTITY Oacute "Ó" ><!-- capital O, acute accent --> 306 <!ENTITY Ocirc "Ô" ><!-- capital O, circumflex accent --> 307 <!ENTITY Otilde "Õ" ><!-- capital O, tilde --> 308 <!ENTITY Ouml "Ö" ><!-- capital O, dieresis or umlaut mark --> 309 <!ENTITY Oslash "Ø" ><!-- capital O, slash --> 310 <!ENTITY Ugrave "Ù" ><!-- capital U, grave accent --> 311 <!ENTITY Uacute "Ú" ><!-- capital U, acute accent --> 312 <!ENTITY Ucirc "Û" ><!-- capital U, circumflex accent --> 313 <!ENTITY Uuml "Ü" ><!-- capital U, dieresis or umlaut mark --> 314 <!ENTITY Yacute "Ý" ><!-- capital Y, acute accent --> 315 <!ENTITY THORN "Þ" ><!-- capital THORN, Icelandic --> 316 <!ENTITY szlig "ß" ><!-- small sharp s, German (sz ligature) --> 317 <!ENTITY agrave "à" ><!-- small a, grave accent --> 318 <!ENTITY aacute "á" ><!-- small a, acute accent --> 319 <!ENTITY acirc "â" ><!-- small a, circumflex accent --> 320 <!ENTITY atilde "ã" ><!-- small a, tilde --> 321 <!ENTITY auml "ä" ><!-- small a, dieresis or umlaut mark --> 322 <!ENTITY aring "å" ><!-- small a, ring --> 323 <!ENTITY aelig "æ" ><!-- small ae diphthong (ligature) --> 324 <!ENTITY ccedil "ç" ><!-- small c, cedilla --> 325 <!ENTITY egrave "è" ><!-- small e, grave accent --> 326 <!ENTITY eacute "é" ><!-- small e, acute accent --> 327 <!ENTITY ecirc "ê" ><!-- small e, circumflex accent --> 328 <!ENTITY euml "ë" ><!-- small e, dieresis or umlaut mark --> 329 <!ENTITY igrave "ì" ><!-- small i, grave accent --> 330 <!ENTITY iacute "í" ><!-- small i, acute accent --> 331 <!ENTITY icirc "î" ><!-- small i, circumflex accent --> 332 <!ENTITY iuml "ï" ><!-- small i, dieresis or umlaut mark --> 333 <!ENTITY eth "ð" ><!-- small eth, Icelandic --> 334 <!ENTITY ntilde "ñ" ><!-- small n, tilde --> 335 <!ENTITY ograve "ò" ><!-- small o, grave accent --> 336 <!ENTITY oacute "ó" ><!-- small o, acute accent --> 337 <!ENTITY ocirc "ô" ><!-- small o, circumflex accent --> 338 <!ENTITY otilde "õ" ><!-- small o, tilde --> 339 <!ENTITY ouml "ö" ><!-- small o, dieresis or umlaut mark --> 340 341 <!ENTITY oslash "ø" ><!-- small o, slash --> 342 <!ENTITY ugrave "ù" ><!-- small u, grave accent --> 343 <!ENTITY uacute "ú" ><!-- small u, acute accent --> 344 <!ENTITY ucirc "û" ><!-- small u, circumflex accent --> 345 <!ENTITY uuml "ü" ><!-- small u, dieresis or umlaut mark --> 346 <!ENTITY yacute "ý" ><!-- small y, acute accent --> 347 <!ENTITY thorn "þ" ><!-- small thorn, Icelandic --> 348 <!ENTITY yuml "ÿ" ><!-- small y, dieresis or umlaut mark -->
Articles.java
1 public class Articles { 2 private String author; 3 private String title; 4 private String pages; 5 private String year; 6 private String volume; 7 private String journal; 8 private String number; 9 private String ee; 10 private String url; 11 public String getAuthor() { 12 return author; 13 } 14 public void setAuthor(String author) { 15 this.author = author; 16 } 17 public String getTitle() { 18 return title; 19 } 20 public void setTitle(String title) { 21 this.title = title; 22 } 23 public String getPages() { 24 return pages; 25 } 26 public void setPages(String pages) { 27 this.pages = pages; 28 } 29 public String getYear() { 30 return year; 31 } 32 public void setYear(String year) { 33 this.year = year; 34 } 35 public String getVolume() { 36 return volume; 37 } 38 public void setVolume(String volume) { 39 this.volume = volume; 40 } 41 public String getJournal() { 42 return journal; 43 } 44 public void setJournal(String journal) { 45 this.journal = journal; 46 } 47 public String getNumber() { 48 return number; 49 } 50 public void setNumber(String number) { 51 this.number = number; 52 } 53 public String getEe() { 54 return ee; 55 } 56 public void setEe(String ee) { 57 this.ee = ee; 58 } 59 public String getUrl() { 60 return url; 61 } 62 public void setUrl(String url) { 63 this.url = url; 64 } 65 66 }
DbHelper.java
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.Statement; 6 7 //注意放入数据库驱动的Jar包 8 public class DbHelper { 9 private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 10 private static final String URL = "jdbc:sqlserver://localhost:1433; DatabaseName=Article"; 11 private static final String USER = "article"; 12 private static final String PASSWORD = "123456"; 13 14 private static DbHelper dbHelper = null; 15 16 private Connection connection = null; 17 18 static { 19 try { 20 Class.forName(DRIVER); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 } 25 26 public static DbHelper getDbHelper() throws Exception { 27 if (dbHelper == null) { 28 dbHelper = new DbHelper(); 29 } 30 return dbHelper; 31 } 32 33 private DbHelper() throws Exception { 34 connection = DriverManager.getConnection(URL, USER, PASSWORD); 35 } 36 37 38 public ResultSet executeQuery(String sql) throws Exception { 39 Statement stmt = connection.createStatement(); 40 return stmt.executeQuery(sql); 41 } 42 43 44 public int executeUpdate(String sql) throws Exception { 45 Statement stmt = connection.createStatement(); 46 return stmt.executeUpdate(sql); 47 } 48 49 50 public ResultSet executeQuery(String sql, Object...objects) throws Exception { 51 PreparedStatement stmt = connection.prepareStatement(sql); 52 if (objects != null) { 53 for (int i = 0; i < objects.length; i++) { 54 Object param = objects[i]; 55 stmt.setObject(i+1, param); 56 } 57 } 58 ResultSet result = stmt.executeQuery(); 59 return result; 60 } 61 62 63 public int executeUpdate(String sql, Object...objects) throws Exception { 64 PreparedStatement stmt = connection.prepareStatement(sql); 65 if (objects != null) { 66 for (int i = 0; i < objects.length; i++) { 67 Object param = objects[i]; 68 stmt.setObject(i+1, param); 69 } 70 } 71 int result = stmt.executeUpdate(); 72 return result; 73 } 74 75 76 public void close() { 77 try { 78 if (connection != null && !connection.isClosed()) { 79 connection.close(); 80 } 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } finally { 84 connection = null; 85 dbHelper = null; 86 } 87 } 88 }
MyHandler.java
1 import org.xml.sax.Attributes; 2 import org.xml.sax.SAXException; 3 import org.xml.sax.helpers.DefaultHandler; 4 5 //解析xml并插入数据库 6 public class MyHandler extends DefaultHandler { 7 8 private Articles articles=null; 9 private int order=0; 10 11 @Override 12 public void characters(char[] ch, int start, int length) throws SAXException { 13 String text = new String(ch, start, length); 14 /*System.out.println(text);*/ 15 switch(order){ 16 case 1: 17 if(articles.getAuthor()!=null){ 18 text=articles.getAuthor()+","+text; 19 } 20 articles.setAuthor(text); 21 break; 22 case 2: 23 articles.setTitle(text); 24 break; 25 case 3: 26 articles.setPages(text); 27 break; 28 case 4: 29 articles.setYear(text); 30 break; 31 case 5: 32 articles.setVolume(text); 33 break; 34 case 6: 35 articles.setJournal(text); 36 break; 37 case 7: 38 articles.setNumber(text); 39 break; 40 case 8: 41 articles.setEe(text); 42 break; 43 case 9: 44 articles.setUrl(text); 45 break; 46 } 47 } 48 49 @Override 50 public void endDocument() throws SAXException { 51 System.out.println("文档结束!"); 52 } 53 54 @Override 55 public void endElement(String uri, String localName, String qName) throws SAXException { 56 /*System.out.println("</"+qName+">");*/ 57 if(qName.equals("article")){//结束标记与节点相同 58 try { 59 insert(articles);//数据库插入数据 60 } catch (Exception e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } 64 System.out.println(articles.getAuthor());//位置,在if内才能输出结果 65 articles=null;//清空重新赋值 66 /*order=0;*/ 67 } 68 } 69 70 @Override 71 public void startDocument() throws SAXException { 72 System.out.println("文档开始!"); 73 } 74 75 @Override 76 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 77 /*System.out.println("<"+qName+">");*/ 78 if(qName.equals("article")){//找到article节点 79 articles=new Articles();//实例化article 80 } 81 switch(qName){//表达式的值会与结构中的每个 case 的值做比较。如果存在匹配,则与该 case 关联的代码块会被执行 82 case"author": 83 order=1; 84 break; 85 case"title": 86 order=2; 87 break; 88 case"pages": 89 order=3; 90 break; 91 case"year": 92 order=4; 93 break; 94 case"volume": 95 order=5; 96 break; 97 case"journal": 98 order=6; 99 break; 100 case"number": 101 order=7; 102 break; 103 case"ee": 104 order=8; 105 break; 106 case"url": 107 order=9; 108 break; 109 110 } 111 } 112 113 private void insert(Articles articles) throws Exception { 114 DbHelper dbHelper=DbHelper.getDbHelper(); 115 String sql="insert into article(author,title,pages,year,volume,journal,number,ee,url) values(?,?,?,?,?,?,?,?,?)"; 116 dbHelper.executeUpdate(sql,articles.getAuthor(),articles.getTitle(),articles.getPages(),articles.getYear(),articles.getVolume(),articles.getJournal(),articles.getNumber(),articles.getEe(),articles.getUrl()); 117 } 118 }
EventHandler.java
1 import org.xml.sax.Attributes; 2 import org.xml.sax.SAXException; 3 import org.xml.sax.helpers.DefaultHandler; 4 5 //单纯解析xml 6 public class EventHandler extends DefaultHandler { 7 8 @Override 9 public void characters(char[] ch, int start, int length) throws SAXException { 10 String text=new String(ch,start,length); 11 System.out.println(text); 12 } 13 14 @Override 15 public void endDocument() throws SAXException { 16 System.out.println("文档结束!"); 17 } 18 19 @Override 20 public void endElement(String uri, String localName, String qName) throws SAXException { 21 System.out.println("</"+qName+">"); 22 } 23 24 @Override 25 public void startDocument() throws SAXException { 26 System.out.println("文档开始!"); 27 } 28 29 @Override 30 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 31 System.out.println("<"+qName+">"); 32 } 33 34 }
SaxTest.java
1 import java.io.File; 2 3 import javax.xml.parsers.SAXParser; 4 import javax.xml.parsers.SAXParserFactory; 5 6 public class SaxTest { 7 8 public static void main(String[] args) { 9 try{ 10 File file=new File("src/dblp.xml"); 11 SAXParserFactory factory=SAXParserFactory.newInstance(); 12 SAXParser saxParser=factory.newSAXParser(); 13 //单纯解析xml 14 //EventHandler handler=new EventHandler(); 15 //解析xml并插入数据库 16 MyHandler handler=new MyHandler(); 17 saxParser.parse(file, handler); 18 }catch(Exception e){ 19 System.out.println(e); 20 } 21 22 } 23 24 }

浙公网安备 33010602011771号