2016-02-16 78 views
0
import java.io.File; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 

public class XMLParser { 

    public void getAllUserNames(String fileName) { 
     try { 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      File file = new File(fileName); 
      if (file.exists()) { 
       Document doc = db.parse(file); 
       Element docEle = doc.getDocumentElement(); 

       // Print root element of the document 
       System.out.println("Root element of the document: " 
         + docEle.getNodeName()); 

       NodeList studentList = docEle.getElementsByTagName("student"); 

       // Print total student elements in document 
       System.out 
         .println("Total students: " + studentList.getLength()); 

       if (studentList != null && studentList.getLength() > 0) { 
        for (int i = 0; i < studentList.getLength(); i++) { 

         Node node = studentList.item(i); 

         if (node.getNodeType() == Node.ELEMENT_NODE) { 

          System.out 
            .println("====================="); 

          Element e = (Element) node; 
          NodeList nodeList = e.getElementsByTagName("name"); 
          System.out.println("Name: " 
            + nodeList.item(0).getChildNodes().item(0) 
              .getNodeValue()); 

          nodeList = e.getElementsByTagName("grade"); 
          System.out.println("Grade: " 
            + nodeList.item(0).getChildNodes().item(0) 
              .getNodeValue()); 

          nodeList = e.getElementsByTagName("age"); 
          System.out.println("Age: " 
            + nodeList.item(0).getChildNodes().item(0) 
              .getNodeValue()); 
         } 
        } 
       } else { 
        System.exit(1); 
       } 
      } 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 
    public static void main(String[] args) { 

     XMLParser parser = new XMLParser(); 
     parser.getAllUserNames("C:/Users/rasika_patil01/Desktop/205_008_JAXBTesterOld/Student.xml"); 
    } 
} 

使用此代碼,我得到一個輸出:使用DOM解析器讀取XML文件和輸出存儲到MySQL

輸出:文檔的 根元素:學生

學生總數:4

名稱:約翰 等級:B

年齡:12

名稱:瑪麗 等級:A

年齡:11

名稱:西蒙 等級:A

年齡:18

名稱:Rasika 等級:A + 年齡:23​​

我想將上面的輸出存儲到MySQL中。任何人都可以請幫忙?

回答

0
  1. 確保您擁有MySQL的JDBC驅動程序jar,可以從Mysql site或使用依賴關係管理器。
  2. 從Java

    MysqlDataSource dataSource = new MysqlDataSource(); 
    dataSource.setUser(user); 
    dataSource.setPassword(password); 
    dataSource.setServerName(server); 
    dataSource.setDatabaseName(database); 
    Connection conn = dataSource.getConnection(); 
    
    PreparedStatement stmt = conn.prepareStatement("INSERT INTO table (name, grade, age) VALUES (?, ?, ?)"); 
    
    ...then while going throug the XML: 
    
    stmt.setString(1, name); 
    stmt.setString(2, grade); 
    stmt.setString(3, age); 
    stmt.execute(); 
    
    ...at the end: 
    stmt.close(); 
    conn.commit(); 
    conn.close() 
    

實際使用中的數據庫連接參數和數據庫表名,列名,列類型的連接。

相關問題