e5db02ce94b473de4a8328cae97a211693e4be69.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /***************************************************
  2. * Copyright 2009-8-19, -sun,All rights reserved.
  3. * Create date : 2009-8-19
  4. * Author : sun
  5. * 把数据库表生成的XML格式化成INSERT语句
  6. **************************************************/
  7. package com.persistence.databak;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.Vector;
  11. import javax.xml.parsers.DocumentBuilder;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13. import javax.xml.parsers.ParserConfigurationException;
  14. import org.apache.log4j.Logger;
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.Element;
  17. import org.w3c.dom.Node;
  18. import org.w3c.dom.NodeList;
  19. import org.xml.sax.SAXException;
  20. public class XmlToTable{
  21. private final static Logger log = Logger.getLogger(XmlToTable.class);
  22. /**
  23. * 根据从数据库中导出的xml文件生成INSERT语句集合 Creat sqldll.
  24. *
  25. * @param filePath
  26. * the file path
  27. *
  28. * @return the vector< string>
  29. *
  30. * @author sun
  31. * @version 2009-8-19-15:28:20
  32. *
  33. * Creat sqldll.
  34. */
  35. public static Vector<String> creatSQLDLL(String filePath) {
  36. Vector<String> relultVec = new Vector<String>();
  37. DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
  38. try {
  39. File xmlfile = new File(filePath);
  40. if (!xmlfile.exists()) {
  41. log.error("file: " + filePath + " not exist");
  42. }
  43. DocumentBuilder builder = domfactory.newDocumentBuilder();
  44. Document document;
  45. document = builder.parse(xmlfile);
  46. NodeList elementLst = document.getElementsByTagName("TABLES");
  47. Element element = (Element) elementLst.item(0);
  48. if (element.hasChildNodes()) {
  49. NodeList cLst = element.getChildNodes();
  50. for (int i = 0; i < cLst.getLength(); i++) {
  51. Node elcNode = cLst.item(i);
  52. // 数据库表名称
  53. String tbName = elcNode.getNodeName();
  54. if (tbName.startsWith("#"))
  55. continue;
  56. tbName = tbName.substring(0, tbName.length() - 1);
  57. log.info("需要恢复的表:" + tbName);
  58. relultVec.add(0, " DELETE FROM " + tbName);
  59. relultVec.addAll(getSqlByTable(document, tbName));
  60. }
  61. }
  62. } catch (SAXException e) {
  63. e.printStackTrace();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. } catch (ParserConfigurationException e) {
  67. e.printStackTrace();
  68. }
  69. return relultVec;
  70. }
  71. /**
  72. * 获得指定表的XML备份文件的ISERT语句集合 Gets the sql by table.
  73. *
  74. * @param document
  75. * the document
  76. * @param TagName
  77. * the tag name
  78. *
  79. * @return the sql by table
  80. *
  81. * @author sun
  82. * @version 2009-8-19-14:56:46
  83. *
  84. * Gets the sql by table.
  85. */
  86. private static Vector<String> getSqlByTable(Document document, String TagName) {
  87. NodeList eleLst = document.getElementsByTagName(TagName);
  88. ColomObj ColomObj = new ColomObj();
  89. /**
  90. * 推算需要操作的列数据
  91. */
  92. Vector<ColomObj> colVec = new Vector<ColomObj>();
  93. Element element = (Element) eleLst.item(0);
  94. if (element.hasChildNodes()) {
  95. NodeList cLst = element.getChildNodes();
  96. for (int i = 0; i < cLst.getLength(); i++) {
  97. Node elcNode = cLst.item(i);
  98. String colName = elcNode.getNodeName();
  99. if (colName.startsWith("#"))
  100. continue;
  101. ColomObj = new ColomObj();
  102. ColomObj.setColName(colName);
  103. colVec.add(ColomObj);
  104. // log.info("需要恢复的列:" + colName);
  105. }
  106. }
  107. int colVeclen = colVec.size();
  108. /**
  109. * 生成INSERT语句到 values (
  110. */
  111. StringBuffer prexSQL = new StringBuffer(" INSERT INTO " + TagName + " (");
  112. for (int i = 0; i < colVeclen; i++) {
  113. prexSQL.append(colVec.get(i).getColName());
  114. if (i == colVeclen - 1) {
  115. // 最后一个
  116. prexSQL.append(") VALUES (");
  117. } else {
  118. prexSQL.append(",");
  119. }
  120. }
  121. // log.info(prexSQL.toString());
  122. Vector<String> sqlVec = new Vector<String>();
  123. /**
  124. * 生成数据列名称值对应的对象
  125. */
  126. StringBuffer sqlsb = new StringBuffer();
  127. for (int i = 0; i < eleLst.getLength(); i++) {
  128. sqlsb = new StringBuffer();
  129. sqlsb.append(prexSQL);
  130. // 获得指定列的数据值
  131. element = (Element) eleLst.item(i);
  132. for (int j = 0; j < colVeclen; j++) {
  133. ColomObj = new ColomObj();
  134. String colName = colVec.get(j).getColName();
  135. Node eletmp = element.getElementsByTagName(colName).item(0);
  136. String value = null;
  137. if (eletmp.getFirstChild() != null) {
  138. value = eletmp.getFirstChild().getNodeValue();
  139. value = "'" + value + "'";
  140. }
  141. sqlsb.append(value);
  142. if (j == colVeclen - 1) {
  143. // 最后一个
  144. sqlsb.append(")");
  145. } else {
  146. sqlsb.append(",");
  147. }
  148. // log.info(colName + "对应值:" + value);
  149. }
  150. // log.info(sqlsb.toString());
  151. sqlVec.add(sqlsb.toString());
  152. }
  153. return sqlVec;
  154. }
  155. public static void main(String[] args) {
  156. try {
  157. Vector<String> sqlvec = creatSQLDLL("d:\\abl.xml");
  158. for (int i = 0; i < sqlvec.size(); i++) {
  159. log.info(sqlvec.get(i));
  160. }
  161. } catch (Exception e) {
  162. // TODO Auto-generated catch block
  163. e.printStackTrace();
  164. }
  165. }
  166. }