dom4j解析xml

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);
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值