package dao;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import util.Util;
public class Bootxml {
public static void listNodes(Element node) {
if (node.getName().equals("exception")) {
System.out.print("当前节点的名称:" + node.getName());
@SuppressWarnings("unchecked")
List<Attribute> list = node.attributes();
String tableName = "";
Map<String, String> columns = new HashMap<String, String>();
// 遍历属性节点
for (Attribute attribute : list) {
if (attribute.getName().equals("tagSource")) {
System.out.println("表名" + attribute.getName() + ":" + attribute.getValue());
// 获取表名
tableName = attribute.getValue();
} else {
System.out.println("字段" + attribute.getName() + ":" + attribute.getValue());
columns.put(attribute.getName(), attribute.getValue());
}
}
// 保存字段名
List<String> columnNames = new ArrayList<String>();
// 保存字段所对应的值
List<String> columnValues = new ArrayList<String>();
// map循环
Set<String> keySet = columns.keySet();
for (String column : keySet) {
columnNames.add(column);
columnValues.add(columns.get(column));
}
// 判断表是否存在
if (!createSelectSql(tableName)) {
// 不存在则拼接创建表语句
StringBuilder createTableSql = new StringBuilder();
createTableSql.append("create table ").append(tableName).append("(");
for (String name : columnNames) {
createTableSql.append(name).append(" varchar(50),");
}
String sql = createTableSql.substring(0, createTableSql.length() - 1);
sql = sql + ")engine=innodb default charset=utf8";
System.out.println("-----------------------------------");
System.out.println(sql);
System.out.println("---------------------------------");
// 创建表
Connection con = Util.getConnection();
Statement st;
try {
st = con.createStatement();
int executeUpdate = st.executeUpdate(sql);// 执行建表语句
System.out.println("----------建表完成");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 取字段的值
// 拼接插入语句
StringBuilder createInsertSql = new StringBuilder();
createInsertSql.append("insert into ").append(tableName);
// for(String name : columnNames) {
// createInsertSql.append(name).append(",");
// }
// String sql=createInsertSql.substring(0,createInsertSql.length()-1)+")";
createInsertSql.append(" value").append("('");
for (String value : columnValues) {
createInsertSql.append(value).append("','");
}
String InsertSql = createInsertSql.substring(0, createInsertSql.length() - 2);
String InSql = InsertSql + ")";
// 插入语句
System.out.println(InSql);
Connection con = Util.getConnection();
Statement st;
try {
st = con.createStatement();
int executeUpdate = st.executeUpdate(InSql);// 执行插入语句
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// System.out.print("当前节点的名称:" + node.getName());
}
// 同时迭代当前节点下面的所有子节点
// 使用递归
Iterator<Element> iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
listNodes(e);
}
}
/**
* @param tableName:表名
* @Description:执行查表语句,判断表是否存在
* @return:
*/
public static boolean createSelectSql(String tableName) {
Connection con = Util.getConnection();
String selectTable = "show tables like '" + tableName + "'";
Statement st;
try {
st = con.createStatement();
ResultSet rs = st.executeQuery(selectTable);
if (rs.next()) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static void main(String[] args) throws DocumentException {
Document doc = new SAXReader().read(new File("C:\\Users\\pc\\Desktop\\exceptionConfig.xml"));
Element rootElement = doc.getRootElement();
listNodes(rootElement);
}
}
dom4j解析xml
最新推荐文章于 2023-11-20 11:43:51 发布