1
|
/**
|
2
|
* Name: DBElement.java
|
3
|
* Purpose: A Class that represents an XML element and can
|
4
|
* write its contents to a database connection
|
5
|
* Institution: National Center for Ecological Analysis and Synthesis
|
6
|
* Copyright: 1998
|
7
|
* Authors: Matt Jones
|
8
|
*
|
9
|
* Version: '$Id: DBElement.java 8 1999-09-13 07:23:05Z jones $'
|
10
|
*/
|
11
|
|
12
|
//package project;
|
13
|
|
14
|
import com.sun.xml.tree.ElementNode;
|
15
|
|
16
|
import org.w3c.dom.DOMException;
|
17
|
import org.w3c.dom.Element;
|
18
|
import org.w3c.dom.Node;
|
19
|
import org.w3c.dom.NodeList;
|
20
|
import org.w3c.dom.ProcessingInstruction;
|
21
|
import org.w3c.dom.Text;
|
22
|
|
23
|
import java.sql.*;
|
24
|
import java.io.IOException;
|
25
|
|
26
|
|
27
|
public class DBElement extends ElementNode {
|
28
|
|
29
|
public DBElement () {
|
30
|
super();
|
31
|
};
|
32
|
|
33
|
/**
|
34
|
* Writes this element and all of its children out to the
|
35
|
* given database connection
|
36
|
*/
|
37
|
public void writeXmlToDB(Connection conn) throws IOException {
|
38
|
/*
|
39
|
if (attributes != null)
|
40
|
attributes.writeXml(conn);
|
41
|
*/
|
42
|
int maxid=0;
|
43
|
Statement stmt;
|
44
|
try {
|
45
|
stmt = conn.createStatement();
|
46
|
stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
|
47
|
try {
|
48
|
ResultSet rs = stmt.getResultSet();
|
49
|
try {
|
50
|
boolean tableHasRows = rs.next();
|
51
|
if (tableHasRows) {
|
52
|
try {
|
53
|
maxid = rs.getInt(1);
|
54
|
} catch (SQLException e) {
|
55
|
System.out.println("Error with getInt: " + e.getMessage());
|
56
|
}
|
57
|
}
|
58
|
} catch (SQLException e) {
|
59
|
System.out.println("Error with next: " + e.getMessage());
|
60
|
}
|
61
|
} catch (SQLException e) {
|
62
|
System.out.println("Error with getrset: " + e.getMessage());
|
63
|
}
|
64
|
stmt.close();
|
65
|
} catch (SQLException e) {
|
66
|
System.out.println("Error getting id: " + e.getMessage());
|
67
|
}
|
68
|
|
69
|
try {
|
70
|
PreparedStatement pstmt = conn.prepareStatement(
|
71
|
"INSERT INTO xml_elements(nodeid, nodename) values (?, ?)");
|
72
|
|
73
|
// Add an element as element number 1500
|
74
|
pstmt.setInt(1, maxid+1); // The first ? is for NODEID
|
75
|
pstmt.setString(2, getTagName());// The second ? is for NODENAME
|
76
|
// Do the insertion
|
77
|
pstmt.execute();
|
78
|
pstmt.close();
|
79
|
|
80
|
} catch (SQLException e) {
|
81
|
System.out.println(e.getMessage());
|
82
|
}
|
83
|
|
84
|
if (hasChildNodes())
|
85
|
writeChildrenXmlToDB(conn);
|
86
|
|
87
|
System.out.println("ID: " + maxid + "\tTag: " + getTagName());
|
88
|
}
|
89
|
|
90
|
public void writeChildrenXmlToDB(Connection conn) throws IOException {
|
91
|
|
92
|
NodeList children = getChildNodes();
|
93
|
if (children != null) {
|
94
|
for (int i = 0; i < children.getLength(); i++) {
|
95
|
if (children.item(i) instanceof ElementNode) {
|
96
|
((DBElement)children.item(i)).writeXmlToDB(conn);
|
97
|
} else {
|
98
|
System.out.println(" Text Node skipped.");
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
|
104
|
// used by JTree to display this node
|
105
|
public String toString ()
|
106
|
{
|
107
|
StringBuffer value = new StringBuffer ();
|
108
|
value.append ('<');
|
109
|
value.append (getTagName ());
|
110
|
value.append (getAttributes ().toString ());
|
111
|
value.append ('>');
|
112
|
return value.toString ();
|
113
|
}
|
114
|
}
|