解析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<XmlMetaNode> 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<XmlMetaNode> 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<root>\n" +
                "    <dog>\n" +
                "        <name>Alexia</name>\n" +
                "        <age>23</age>\n" +
                "        <sex>Female</sex>\n" +
                "    </dog>\n" +
                "</root>";


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

    }

}

猜你喜欢

转载自blog.csdn.net/slq_jobs/article/details/78321362