/**
*解析XML文件生成一个含有可执行SQL语句的字符串
*@paramxmlFileName
*@returnSQL
*/
public String parseXmltoSQL(String xmlFileName) {
StringBuffer sbsql = new StringBuffer();
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new File(xmlFileName));
Element element = document.getRootElement();
Iterator it = element.elementIterator("table");
while (it.hasNext()) {
element = (Element) it.next();
//获得对表的操作
String oper = element.attributeValue("operation");
//获得表名
String tableName = element.attributeValue("name");
if ("1".equals(oper)) {
sbsql.append("insert into ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
String columnName2 = null;
String columnValue2 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName2 = element.attributeValue("name");
columnValue2 = element.getText();
}
sbsql.append("("+columnName1+","+columnName2+")"+" values('"+columnValue1+"','"+columnValue2+"')\n");
} elseif ("2".equals(oper)) {
sbsql.append("update ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
String columnName2 = null;
String columnValue2 = null;
String columnName3 = null;
String columnValue3 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName2 = element.attributeValue("name");
columnValue2 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName3 = element.attributeValue("name");
columnValue3 = element.getText();
}
sbsql.append(" set "+columnName2+"='"+columnValue2+"',"+columnName3+"='"+columnValue3+"' where "+columnName1+"="+columnValue1+"\n");
}elseif ("3".equals(oper)) {
sbsql.append("delete from ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
sbsql.append(" where "+columnName1+"="+columnValue1);
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sbsql.toString();
}
l JSP/SERVLET |