用DOM解析XML文件并存入mysql数据库

对于一个数据量大、复杂的xml文件,要进行解析并且存入数据库。首先要对xml文件的dom结构有所了解,并进行分析,然后获取自己想要的数据,之后进行数据的存储。

接下来说明一下具体的步骤和方法。

一、解析xml文件

我的xml文件(这里只是列出了一部分)如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <person>  
  3.     <personID>22097</personID>  
  4.     <FullName>Ajay Gupta</FullName>  
  5.     <FirstName>Ajay</FirstName>  
  6.     <LastName>Gupta</LastName>  
  7.     <publication>  
  8.         <title>Explanation-based Failure Recovery</title>  
  9.         <year>1987</year>  
  10.         <authors>Ajay Gupta</authors>  
  11.         <jconf>AAAI</jconf>  
  12.         <id>13048</id>  
  13.         <label>0</label>  
  14.         <organization>null</organization>  
  15.     </publication>  
  16.     <publication>  
  17.         <title>Time Representation in Prolog Circuit Modelling</title>  
  18.         <year>1991</year>  
  19.         <authors>Yossi Lichtenstein,Bob Welham,Ajay Gupta</authors>  
  20.         <jconf>ALPUK</jconf>  
  21.         <id>39153</id>  
  22.         <label>0</label>  
  23.         <organization>null</organization>  
  24.     </publication><pre name="code" class="html"></person>  
 

 
我想要的数据是publication中的id和authors,并且将id和authors都存为String格式,我的解析代码如下: 
 

我创建了一个publication类,方便将数据存储:

Publication类:

[java]  view plain  copy
  1. public class Publication {    
  2.     private String id;    
  3.     private String authors;    
  4.   
  5.     public String getAuthors() {  
  6.         return authors;  
  7.     }  
  8.     public void setAuthors(String authors) {  
  9.         this.authors = authors;  
  10.     }  
  11.     public String getId() {    
  12.         return id;    
  13.     }    
  14.     public void setId(String string) {    
  15.         this.id = string;    
  16.     }    
  17.       
  18.     @Override    
  19.     public String toString(){    
  20.         return this.id+":"+this.authors;    
  21.     }    
  22. }  

解析代码:

[java]  view plain  copy
  1. private static ArrayList<Publication> getParserAuthor() {  
  2.         ArrayList<Publication> list= new ArrayList<Publication>();  
  3.         //获取DOM解析器  
  4.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  5.         DocumentBuilder builder;  
  6.         try {  
  7.                builder = factory.newDocumentBuilder();  
  8.                Document doc;  
  9.                doc = builder.parse(new File("Ajay Gupta.xml"));  
  10.                    //得到一个element根元素,获得根节点  
  11.                 Element root = doc.getDocumentElement();          
  12.                 System.out.println("根元素:"+root.getNodeName());  
  13.                   
  14.                 //子节点  
  15.                 NodeList personNodes = root.getElementsByTagName("publication");  
  16.                 for(int i = 0; i<personNodes.getLength();i++){  
  17.                     Element personElement = (Element) personNodes.item(i);  
  18.                     Publication publication = new Publication();  
  19.                     NodeList childNodes = personElement.getChildNodes();  
  20.                     //System.out.println("*****"+childNodes.getLength());    
  21.                       
  22.                     for (int j = 0; j < childNodes.getLength(); j++) {  
  23.                     if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){  
  24.                         if("authors".equals(childNodes.item(j).getNodeName())){  
  25.                             publication.setAuthors(childNodes.item(j).getFirstChild().getNodeValue());  
  26.                         }else if("id".equals(childNodes.item(j).getNodeName())){  
  27.                             publication.setId(childNodes.item(j).getFirstChild().getNodeValue());  
  28.                         }  
  29.                     }  
  30.                 }  
  31.                     list.add(publication);  
  32.                 }  
  33.                 for(Publication publication2 : list){  //为了查看数据是否正确,进行打印结果  
  34.                      System.out.println(publication2.toString());    
  35.                  }   
  36.         } catch (SAXException e) {  
  37.             e.printStackTrace();  
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();          
  40.         } catch (ParserConfigurationException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         return list;  
  44.              
  45.     }  
二、存入数据库
1)我创建的数据库表结构为:

Publication表:



设置id的目的是让数据能够自增,记得要设置为主键并且是不为空。如果没有客户端可以创建数据库和表,可以直接在命令行里写也是很简单的。

2)插入数据库。

在进行数据库操作时,需要导入连接数据库的驱动,并测试是否连接成功。

[java]  view plain  copy
  1. static String sqlStr = "jdbc:mysql://localhost:3306/Publications";  
  2.     static String rootName = "root";//数据库名  
  3.     static String rootPwd = "root";//数据库密码  
  4.   
  5.     public static void writeToMysql(Publication publication) {  
  6.         System.out.println(publication);  
  7.         //1.加载driver驱动  
  8.         try {  
  9.             // 加载MySql的驱动类  
  10.             Class.forName("com.mysql.jdbc.Driver");  
  11.         } catch (ClassNotFoundException e) {  
  12.             System.out.println("找不到驱动程序类 ,加载驱动失败!");  
  13.             e.printStackTrace();  
  14.         }  
  15.         //2.建立连接  
  16.         Statement st = null;  
  17.         //调用DriverManager对象的getConnection()方法,获得一个Connection对象  
  18.         Connection con  =null;  
  19.         try {  
  20.             //建立数据库连接  
  21.             con = DriverManager.getConnection(sqlStr, rootName,rootPwd);  
  22.             //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)  
  23.             String paperId= publication.getId();  
  24.             String authors = publication.getAuthors();  
  25.             //插入语句格式  
  26.             String sql = "insert into publication1(paperId,Authors) values(\""+paperId+"\",\""+authors+"\")";  
  27.             System.out.println(sql);  
  28.             st =  con.createStatement(); //创建一个Statement对象  
  29.             st.executeUpdate(sql);//提交数据更新  
  30.         } catch (SQLException e) {  
  31.             e.printStackTrace();  
  32.         }finally{  
  33.             try {  
  34.                 st.close();  
  35.                 con.close();  
  36.             } catch (SQLException e) {  
  37.                 e.printStackTrace();  
  38.             }  
  39.         }  
  40.     }  
3)测试代码,编码main函数进行调用。

[java]  view plain  copy
  1. public static void main(String[] args) {  
  2.         ArrayList<Publication> list= new ArrayList<Publication>();  
  3.         list = getParserAuthor();  
  4.         for(int i=0;i<list.size();i++)  
  5.         {             
  6.             if (list.get(i)!=null)  
  7.             writeToMysql(list.get(i));  
  8.         }  
  9.     }  
到这里所有的步骤就结束了。
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值