解析xml生成建表sql

package com.shuquanlin.domain;

import lombok.Data;

/**
 * Created by sql on 10/23/17.
 */
@Data
public class XmlMetaNode {

    /**
     * 列名
     */
    private String name;

    /**
     * 列数据类型
     */
    private String valueType;

}

package com.shuquanlin.util;

import com.shuquanlin.contact.SqlSentence;
import com.shuquanlin.domain.XmlMetaNode;

import java.util.List;

import static com.shuquanlin.util.JsonSqlUtil.getType;

/**
 * Created by sql on 10/23/17.
 */
public class XmlSqlUtil {

    /**
     * 建表语句
     * @author sql
     * @date 10/23/17 5:00 PM
     */
    public static String sqlCreateTable(String tableName , List xmlMetaNodeList){


        String sqlCreate = "CREATE TABLE " + tableName + "(\n" + getRowName(xmlMetaNodeList);





        return sqlCreate;
    }

    /**
     * 获取列名
     * @author sql
     * @date 10/23/17 5:05 PM
     */
    public static String getRowName(List xmlMetaNodeList){

        StringBuffer sqlRowNameBuffer = new StringBuffer();

        for (XmlMetaNode xmlMetaNode:xmlMetaNodeList) {

            String key = xmlMetaNode.getName();
            String valueType = xmlMetaNode.getValueType();

            String type = getType(valueType);

            sqlRowNameBuffer.append(key).append(" ").append(type).append(" ").append("CHARACTER SET utf8 NULL ,");

        }
        sqlRowNameBuffer.deleteCharAt(sqlRowNameBuffer.length()-1);
        sqlRowNameBuffer.append(")");
        String sqlRowName = sqlRowNameBuffer.toString();
        return sqlRowName;



    }
}



package com.shuquanlin;

import com.shuquanlin.domain.XmlMetaNode;
import com.shuquanlin.util.XmlSqlUtil;
import org.assertj.core.util.Lists;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.List;

/**
 * Created by sql on 10/23/17.
 */
public class XmlTest {
    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Test
    public void test1(){


        String xml = "\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\n" +
                "    \n" +
                "        Alexia\n" +
                "        23\n" +
                "        Female\n" +
                "    \n" +
                "";


        List xmlMetaNodeList = Lists.newArrayList();
        try {

            Document document = DocumentHelper.parseText(xml);
            Element users = document.getRootElement();
            for (Iterator i = users.elementIterator(); i.hasNext();) {
                Element element = (Element) i.next();
                for (Iterator j = element.elementIterator(); j.hasNext();) {
                    XmlMetaNode xmlMetaNode = new XmlMetaNode();
                    Element node = (Element) j.next();

                    String key = node.getName();
                    String valueType = node.getText().getClass().getName();
                    xmlMetaNode.setName(key);
                    xmlMetaNode.setValueType(valueType);
                    xmlMetaNodeList.add(xmlMetaNode);

                }
            }

            String createTableSql = XmlSqlUtil.sqlCreateTable("shuquanlin1", xmlMetaNodeList);
            System.out.println(createTableSql);

        } catch (DocumentException e) {
            logger.error(e.getMessage(),e);
        }

    }

}

你可能感兴趣的:(xml,java,数据库)