package com.sc;
import java.util.ArrayList;
import java.util.List;
public class Article {
private List<String> authors;
private String title;
private String pages;
private Integer year;
private Integer volume;
private String journal;
private String number;
private String ee;
private String url;
public Article() {
authors = new ArrayList<>();
}
public Article(List<String> authors, String title, String pages, Integer year, Integer volume, String journal,
String number, String ee, String url) {
this.authors = authors;
this.title = title;
this.pages = pages;
this.year = year;
this.volume = volume;
this.journal = journal;
this.number = number;
this.ee = ee;
this.url = url;
}
public List<String> getAuthors() {
return authors;
}
public void setAuthors(List<String> authors) {
this.authors = authors;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPages() {
return pages;
}
public void setPages(String pages) {
this.pages = pages;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getVolume() {
return volume;
}
public void setVolume(Integer volume) {
this.volume = volume;
}
public String getJournal() {
return journal;
}
public void setJournal(String journal) {
this.journal = journal;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getEe() {
return ee;
}
public void setEe(String ee) {
this.ee = ee;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
package com.sc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 数据库辅助工具类
* @author Administrator
*
*/
public class DbHelper {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://127.0.0.1:1433;Database=information";
private static final String USER = "sc";
private static final String PASSWORD = "sc";
private static DbHelper dbHelper = null;
private Connection connection = null;
static {
try {
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取DbHelper实例
* @return
* @throws Exception
*/
public static DbHelper getDbHelper() throws Exception {
if (dbHelper == null) {
dbHelper = new DbHelper();
}
return dbHelper;
}
/**
* 私有构造方法
* @throws Exception
*/
private DbHelper() throws Exception {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
}
/**
* 执行查询
* @param sql
* @return
* @throws Exception
*/
public ResultSet executeQuery(String sql) throws Exception {
Statement stmt = connection.createStatement();
return stmt.executeQuery(sql);
}
/**
* 执行更新
* @param sql
* @return
* @throws Exception
*/
public int executeUpdate(String sql) throws Exception {
Statement stmt = connection.createStatement();
return stmt.executeUpdate(sql);
}
/**
* 执行条件查询
* @param sql
* @param objects 参数
* @return
* @throws Exception
*/
public ResultSet executeQuery(String sql, Object...objects) throws Exception {
PreparedStatement stmt = connection.prepareStatement(sql);
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
Object param = objects[i];
stmt.setObject(i+1, param);
}
}
ResultSet result = stmt.executeQuery();
return result;
}
/**
* 根据参数执行跟新操作
* @param sql SQL语句
* @param objects 参数值
* @return
* @throws Exception
*/
public int executeUpdate(String sql, Object...objects) throws Exception {
PreparedStatement stmt = connection.prepareStatement(sql);
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
Object param = objects[i];
stmt.setObject(i+1, param);
}
}
int result = stmt.executeUpdate();
return result;
}
/**
* 关闭操作
*/
public void close() {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection = null;
dbHelper = null;
}
}
}
package com.sc;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
private List<Article> articles = new ArrayList<>();
private Article article = null;
private int propertyOrder = 0;
List<String> author = null;
@Override
public void startDocument() throws SAXException {
System.out.println("文档开始");
}
@Override
public void endDocument() throws SAXException {
System.out.println("文档结束");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("article")) {
article = new Article();
return;
}
switch (qName) {
case "author":
propertyOrder = 1;
break;
case "title":
propertyOrder = 2;
break;
case "pages":
propertyOrder = 3;
break;
case "year":
propertyOrder = 4;
break;
case "volume":
propertyOrder = 5;
break;
case "journal":
propertyOrder = 6;
break;
case "number":
propertyOrder = 7;
break;
case "ee":
propertyOrder = 8;
break;
case "url":
propertyOrder = 9;
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("article")) {
articles.add(article);
article = null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String content = new String(ch, start, length);
switch (propertyOrder) {
case 1:
if (author == null) {
author = new ArrayList<>();
}
author.add(content);
break;
case 2:
if (author == null) {
author = new ArrayList<>();
author.add(0, "");
}
article.setAuthors(author);
//System.out.println(author == null);
article.setTitle(content);
author = null;
break;
case 3:
article.setPages(content);
break;
case 4:
article.setYear(Integer.parseInt(content));
break;
case 5:
article.setVolume(Integer.parseInt(content));
break;
case 6:
article.setJournal(content);
break;
case 7:
article.setNumber(content);
break;
case 8:
article.setEe(content);
break;
case 9:
article.setUrl(content);
break;
}
}
List<Article> getArticles() {
return articles;
}
}
package com.sc;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class SAXParserDemo {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
MyHandler handler = new MyHandler(); // 使用自定义Handler
parser.parse("dblp/dblp.xml", handler);
System.out.println("Done.");
List<Article> articles = handler.getArticles();
DbHelper dbHelper = DbHelper.getDbHelper();
int len = articles.size();
for (int i = 0;i < len;++i) {
Article article = articles.get(i);
StringBuffer authorsName = new StringBuffer();
for (int j = 0;j < article.getAuthors().size();++j) {
if (j > 0) {
authorsName.append(",");
}
authorsName.append(article.getAuthors().get(j));
}
String authorName = authorsName.toString();
dbHelper.executeUpdate("insert into data(authors, title, pages, year, volume, journal, number, ee, url) values(?, ?, ?, ?, ?, ?, ?, ?, ?)",
authorName, article.getTitle(), article.getPages(), article.getYear(), article.getVolume(), article.getJournal(), article.getNumber(), article.getEe(), article.getUrl());
}
dbHelper.close();
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}