-
Notifications
You must be signed in to change notification settings - Fork 7
/
XMLtoSQL_Java.java
78 lines (59 loc) · 2.73 KB
/
XMLtoSQL_Java.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import javax.xml.parsers.*;
import java.sql.* ;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.xml.sax.*;
import org.w3c.dom.*;
public class XMLtoSQL_Java {
public static void main(String[] args) {
String currentDir = new File("").getAbsolutePath();
String database = currentDir + "/CLData.db";
// XML OBJECTS
Document dom;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// CONNECT TO DB
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+database);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM cldata");
String insertSQL = "INSERT INTO cldata (`user`, `city`, `category`, `post`, `link`, `time`)"
+ "VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insertSQL);
// PARSE XML
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(currentDir + "/CLData.xml");
Element doc = dom.getDocumentElement();
NodeList mcNodes = doc.getElementsByTagName("missedConnection");
// EXTRACT BY NODE
for (int i=0; i < mcNodes.getLength(); i++) {
NodeList childNodes = mcNodes.item(i).getChildNodes();
int j = 1;
for (int n=0; n < childNodes.getLength(); n++) {
if(childNodes.item(n).getNodeType() == Node.ELEMENT_NODE){
Element cElem = (Element) childNodes.item(n);
pstmt.setString(j, cElem.getTextContent());
j = j + 1;
}
}
// APPEND DATA
pstmt.executeUpdate();
}
conn.commit();
pstmt.close();
conn.close();
System.out.println("Successfully migrated XML data into SQL database!");
} catch (FileNotFoundException ffe) {
System.out.println(ffe.getMessage());
} catch (ParserConfigurationException pce) {
System.out.println(pce.getMessage());
} catch (SAXException se) {
System.out.println(se.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} catch (SQLException err) {
System.out.println(err.getMessage());
}
}
}