码迷,mamicode.com
首页 > 数据库 > 详细

JAVA之XML文件解析(根据头部和身体自动组合成SQL)

时间:2020-04-04 16:11:25      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:next   sele   help   otf   comm   replace   tor   int()   play   

package com.citic.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.citic.supis.domain.SqlEntityStatment;
import com.citic.util.comm.CommFun;
import com.citic.util.comm.IConstants;

public class XMLUtil implements IConstants{
    private static String oldtag = "";
    private static String xmlstr = "", xmlheads = "";
    private static int debuglevel = 0;

    public static void xmlCreate(String tmpstr, String createxmlfile) {
        if(tmpstr.length()<minxmllength){
            return;    
        }
        try {
            xmlCreate(DocumentHelper.parseText(tmpstr), createxmlfile);
        } catch (DocumentException e) {
            CommFun.log(ERR, "根据字符串创建XML文档出错开始");
            CommFun.log(ERR,tmpstr);
            e.printStackTrace();
            CommFun.log(ERR, "根据字符串创建XML文档出错信息结束");
        }
    }
    
    
    public static void xmlCreate(Document document, String createxmlfile) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        String encod = "UTF-8";
        format.setEncoding(encod);
        int bytes = 0;
        format.setNewLineAfterDeclaration(false);
        String pthseparator = System.getProperty("file.separator");
        File f = new File(createxmlfile.substring(0,
                createxmlfile.lastIndexOf(pthseparator)));
        CommFun.log(debuglevel, f.getParent()+","+createxmlfile);
        if (!f.exists()) {
            f.mkdirs();
        }

        FileOutputStream fos;
        try {
            bytes = document.asXML().getBytes(encod).length;
            fos = new FileOutputStream(createxmlfile);
            XMLWriter writer= new XMLWriter(fos, format);
            writer.write(document);
            writer.close();
            fos.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        CommFun.log(debuglevel, "xmlcreate:" + createxmlfile + ":" + bytes
                + ":");
    }

    public static SqlEntityStatment dispElementshead(Document doc,
            HashMap nsMap, String tagname) {
        String xmlstr = "", xmlheads = "";
        SqlEntityStatment ses1 = new SqlEntityStatment();
        XPath xsub = doc.createXPath("//ns:" + tagname);
        xsub.setNamespaceURIs(nsMap);
        List list = xsub.selectNodes(doc);
        Iterator itr = list.iterator();
        int leafcnt = 0, elecnt = 0;
        String oldtag = "", newtag = "";
        for (int i = 0; i < 1; i++) {
            Element pele = (Element) itr.next();
            Iterator itrs = pele.elements().iterator();
            elecnt = pele.elements().size();
            while (itrs.hasNext()) {
                Element ele = (Element) itrs.next();
                newtag = ele.getName();
                if (!"".equals(ele.getTextTrim())) {
                    xmlstr += ",‘" + ele.getTextTrim() + "‘";
                    xmlheads += "," + ele.getName();
                    if (oldtag.equals(newtag)) {
                    }
                    leafcnt++;
                }
                oldtag = newtag;
            }
        }
        /*
         * defaultsep=subi>0?",":"@"; && elecnt != leafcnt
         */
        if (xmlstr.length() > 0 && list.size() == 1) {
            ses1.setSqlstr(xmlstr.substring(1).trim());
            ses1.setSqlfileds(xmlheads.substring(1).trim());
        }
        // System.out.println("ses1:[" + ses1 + "]");
        CommFun.log(debuglevel, "[" + ses1 + "]");
        xmlstr = "";
        xmlheads = "";
        return ses1; // .substring(1).split(defaultsep);
    }

    /**
     * 适合对并列的并且以下都可以行成一列的
     * 
     * @param ele
     */
    public static SqlEntityStatment displayAllEle(Element ele) {
        Iterator itr = ele.elements().iterator();
        Element element;
        String tagname, text, sf = "";
        SqlEntityStatment sestmp = null;
        while (itr.hasNext()) {
            element = (Element) itr.next();
            tagname = element.getName();
            text = element.getTextTrim();
            if ("".equals(text)) {
                displayAllEle(element);
                // xmlstr = "";
                // xmlheads = "";
            } else {
                if (!oldtag.equals(tagname)) {
                    xmlheads += "," + tagname;
                } else {
                    sf = "@";
                }
                xmlstr += "," + sf + text;
                oldtag = tagname;
            }
        }
        if (!"".equals(xmlstr)) {
            String xmltmp = xmlstr.substring(1);
            xmltmp = xmltmp.replace(",@", "@");
            xmltmp = "‘" + xmltmp.replace(",", "‘,‘") + "‘";
            // sestmp.setSqlfileds(xmlheads);
            // sestmp.setSqlstr(xmltmp);
            sestmp = new SqlEntityStatment(xmlheads.substring(1), xmltmp);
            // 张明伟 测试20170407 START
            // System.out.println("sestmp:[" + sestmp + "]");
            // System.out.println("xmltmp:[" + xmltmp + "]");
            // System.out.println("xmlheads:[" + xmlheads + "]");
            // 张明伟 测试20170407 END
        }
        return sestmp;
    }

    /**
     * 此功能只处理并列多行的数据,如产生DiffList多行数据
     * 
     * @param doc
     * @param nsMap
     * @param tagname
     */
    public static SqlEntityStatment[] dispElements1(Document doc,
            HashMap nsMap, String tagname) {
        SqlEntityStatment[] sestmps = null;
        XPath xsub = doc.createXPath("//ns:" + tagname);
        xsub.setNamespaceURIs(nsMap);
        List list = xsub.selectNodes(doc);
        Iterator itr = list.iterator();
        int listsize = list.size();
        if (listsize < 1) {
            return null;
        }
        sestmps = new SqlEntityStatment[listsize];
        for (int i = 0; i < listsize; i++) {
            xmlstr = "";
            xmlheads = "";
            Element pele = (Element) itr.next();
            sestmps[i] = new SqlEntityStatment(displayAllEle(pele));
            // 张明伟 测试20170407
            CommFun.log(debuglevel, "...." + sestmps[i]);
            // 张明伟 测试20170407
        }
        return sestmps;
    }

    public static SqlEntityStatment[] mergedata(SqlEntityStatment sestmp1,
            SqlEntityStatment[] sestmps1) {
        if (sestmps1 == null && sestmp1 == null) {
            return null;
        } else if (sestmps1 == null) {
            return new SqlEntityStatment[] { sestmp1 };
        } else if (sestmp1 == null) {
            return sestmps1;
        }
        
        int len = sestmps1.length;
        SqlEntityStatment[] sestmps2 = new SqlEntityStatment[len];
        String tmpfld = "", tmpstr = "";
        for (int i = 0; i < len ; i++) {
            tmpfld = "";
            tmpstr = "";
            if(sestmps1[i]!=null){
                tmpfld = sestmp1.getSqlfileds() + "," + sestmps1[i].getSqlfileds();
                tmpstr = sestmp1.getSqlstr() + "," + sestmps1[i].getSqlstr();
            }
            sestmps2[i] = new SqlEntityStatment(tmpfld, tmpstr);
            // 张明伟 测试20170407
            CommFun.log(debuglevel, "[" + i + "]:" + sestmps2[i]);
            // 张明伟 测试20170407
        }
        return sestmps2;
    }
}

 

JAVA之XML文件解析(根据头部和身体自动组合成SQL)

标签:next   sele   help   otf   comm   replace   tor   int()   play   

原文地址:https://www.cnblogs.com/silencemaker/p/12632223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!