如何使用dom解析,将xml数据导入数据库中呢?
1、项目准备,建立lib文件,这里用的是mysql数据库,将mysql用的jar包,添加到项目中
2、项目下建一个文件夹,用来放待解析的xml文件。
3、根据xml描述建立一个article对象;
package com.dom; public class Article { private String author1; private String author2; private String author3; private String title; private String pages; private String year; private String volume; private String journal; private String number; private String ee; private String url; public String getAuthor1() { return author1; } public void setAuthor1(String author1) { this.author1 = author1; } public String getAuthor2() { return author2; } public void setAuthor2(String author2) { this.author2 = author2; } public String getAuthor3() { return author3; } public void setAuthor3(String author3) { this.author3 = author3; } 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 String getYear() { return year; } public void setYear(String content) { this.year = content; } public String getVolume() { return volume; } public void setVolume(String content) { this.volume = content; } 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; } }
4、写一个数据库操纵类
package sax; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class DbHelper{ private static final String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=dblp"; private static final String USER = "root"; private static final String PASSWORD = "x5"; private static Connection conn = null; private static Statement stmt = null; private static DbHelper helper = null; static { try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch (Exception e) { e.printStackTrace(); } } private DbHelper() throws Exception{ conn = DriverManager.getConnection(URL,USER,PASSWORD); stmt = conn.createStatement(); } public static DbHelper getDbHelper() throws Exception{ if(helper == null || conn == null || conn.isClosed()){ helper = new DbHelper(); } return helper; } public ResultSet executeQuery(String sql) throws Exception { if (stmt != null) { return stmt.executeQuery(sql); } throw new Exception("数据库未正常连接"); } public ResultSet executeQuery(String sql, Object...args) throws Exception { if (conn == null || conn.isClosed()) { DbHelper.close(); throw new Exception("数据库未正常连接"); } PreparedStatement ps = conn.prepareStatement(sql); int index = 1; for (Object arg : args) { ps.setObject(index, arg); index++; } return ps.executeQuery(); } public int executeUpdate(String sql) throws Exception { if (stmt != null) { return stmt.executeUpdate(sql); } throw new Exception("数据库未正常连接"); } public int executeUpdate(String sql, Object...args) throws Exception { if (conn == null || conn.isClosed()) { DbHelper.close(); throw new Exception("数据库未正常连接"); } PreparedStatement ps = conn.prepareStatement(sql); int index = 1; for (Object arg : args) { ps.setObject(index, arg); index++; } return ps.executeUpdate(); } public PreparedStatement prepareStatement(String sql) throws Exception { return conn.prepareStatement(sql); } /** * 关闭对象,同时将关闭连接 */ public static void close() { try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } finally { helper = null; } } }
package com.dom; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class domparse { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); DbHelper dbhelper=DbHelper.getDbHelper(); List<Article> articles=new ArrayList<Article>(); try { Document document=builder.parse("dblp/dblp.xml"); System.out.println(document.getNodeName());//获得document 根节点 Element root=document.getDocumentElement();///获取当前节点的子节点 System.out.println(root.getNodeName()); NodeList list=root.getElementsByTagName("article");//根据节点名,找到dblp下面的article个数 int len =list.getLength();//获取长度 for(int i=0;i<len;i++){////遍历这个树形结构 Node child=list.item(i);//依次得到每一个article NodeList childrens=child.getChildNodes();//得到article 下面所有节点 Article article=new Article(); for(int k=0;k<childrens.getLength();k++){//遍历这些节点 Node node=childrens.item(k);//得到这个节点 int zuozhe=1; if(node.getNodeType()==Node.ELEMENT_NODE){//如果这个节点是element节点 String nodename=node.getNodeName();//获取该节点名称 String content=node.getTextContent();//获取该节点文本内容 NamedNodeMap map=node.getAttributes();//获取该元素中的所有属性 if("author".equals(nodename)&&zuozhe==1){ //这里作者有多个,判断是第几个作者 article.setAuthor1(content); zuozhe++; } else if("author".equals(nodename)&&zuozhe==2){ article.setAuthor2(content); } else if("author".equals(nodename)){ article.setAuthor3(content); } if("title".equals(nodename)){ article.setTitle(content); } if("year".equals(nodename)){ article.setYear(content); } if("pages".equals(nodename)){ article.setPages(content); } if("numeber".equals(nodename)){ article.setNumber(content); } if("volume".equals(nodename)){ article.setVolume(content); } if("journal".equals(nodename)){ article.setJournal(content); } if("ee".equals(nodename)){ article.setEe(content); } if("url".equals(nodename)){ article.setUrl(content); } } } articles.add(article);///添加一个article对象到集合中 } String sql = "insert into dblp(author1,author2,author3, title, pages, year, volume, journal, number, ee, url)values(?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)"; System.out.println(articles.size());//查看集合大小是否与xml中数据是否等长 for(int j=0;j<articles.size();j++){//遍历集合,将数据插入数据库中 Article article=articles.get(j); dbhelper.executeUpdate(sql,article.getAuthor1(),article.getAuthor2(),article.getAuthor3(),article.getTitle(),article.getPages(),article.getYear(),article.getVolume(),article.getJournal(),article.getNumber(),article.getEe(),article.getUrl()); } dbhelper.close(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }