//Title: XML-Tabellenimport
//Version:
//Copyright: Copyright (c) 2002
//Author: Altmann
//Company: GTDS
//Description:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Hashtable;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
public class XMLTabImp {
public static void main (String args[])
throws java.io.IOException, SQLException, SAXException {
String usrpass = args[0];
String username = usrpass.substring(0, usrpass.indexOf("/"));
String password = usrpass.substring(usrpass.indexOf("/")+1, usrpass.indexOf("@"));
String database = usrpass.substring(usrpass.indexOf("@")+1);
Document xml;
DOMParser dp = new DOMParser();
dp.parse(args[1]);
xml = dp.getDocument();
Connection conn = DBSession.getConnection(username, password, database);
Hashtable dataTypes = new Hashtable();
ChildElementWalker cew = new ChildElementWalker(xml.getDocumentElement(), null);
Element tab = cew.getNextElement();
String stat = "INSERT INTO " + tab.getTagName();
String colList = "";
String valList = "";
cew = new ChildElementWalker(tab, null);
Element desc = cew.getNextElement();
ChildElementWalker descW = new ChildElementWalker(desc, null);
while(true) {
Element column = descW.getNextElement();
if(column == null) {
break;
};
String colName = column.getTagName();
String dataType = column.getAttribute("datatype");
dataTypes.put(colName, dataType);
colList += ", " + colName;
valList += ", ?";
//System.err.println(colName);
}
stat = stat + "(" + colList.substring(2) + ") VALUES (" + valList.substring(2) + ")";
System.err.println(stat);
PreparedStatement ins = conn.prepareStatement(stat);
int count = 0;
while(true) {
Element row = cew.getNextElement();
if(row == null) {
break;
};
ChildElementWalker colw = new ChildElementWalker(row, null);
int i = 0;
while(true) {
Element column = colw.getNextElement();
if(column == null) {
ins.execute();
ins.clearParameters();
break;
};
String colName = column.getTagName();
// System.err.print(colName);
String dataType = (String) dataTypes.get(colName);
i++;
String value = null;
if(column.getFirstChild() != null) {
value = column.getFirstChild().getNodeValue();
}
if(dataType.equals("DATE")) {
if(value==null) {
ins.setTimestamp(i, null);
} else {
ins.setTimestamp(i, Timestamp.valueOf(value));
}
} else if(dataType.equals("NUMBER")) {
if(value==null) {
ins.setString(i, null);
} else {
ins.setDouble(i, Double.valueOf(value).doubleValue());
}
}else {
ins.setString(i, value);
}
}
count++;
}
System.err.print(count+ " rows inserted");
ins.close();
}
}