package com.ebring.doc;   

   

 import java.io.File; 

 import java.io.FileWriter;   

 import java.io.IOException;   

   

 import java.util.List; 

 import java.util.Map; 


 import org.dom4j.Document;   

 import org.dom4j.DocumentHelper;   

 import org.dom4j.Element;   

 import org.dom4j.io.OutputFormat;   

 import org.dom4j.io.XMLWriter;   


 import com.ebring.common.SimpleJDBCOperator; 

   

 public class CreateSubXML {   

   


 public void main(String[] args) {   

         XMLWriter writer = null;   

         try {   

             //获取项目当前绝对路径 

             File directory = new File("1"); 

             String xdpath = directory.getAbsolutePath(); 

             System.out.println("截取路径:"+xdpath.substring(0,2)); 

             String path = xdpath.substring(0,2); 

             //查询数据库 

              String sql1 = "select id,subdistrict_name from SUBDISTRICT"; 

              //链接数据库 

              SimpleJDBCOperator operator = new SimpleJDBCOperator(); 

              List<Map<String, Object>> list = operator.GetList(sql1); 

              if (list != null && list.size() > 0) { 

                  for (int j = 0; j < list.size(); j++) { 

                          Map<String, Object> map = list.get(j); 

                          String ID = map.get("ID") == null ? "" : map 

                                  .get("ID").toString(); 

                          String SUBDISTRICT_NAME = map.get("SUBDISTRICT_NAME") == null ? "" : map 

                                  .get("SUBDISTRICT_NAME").toString(); 

                          //企业1 拼接查询条件 

                          String sql2 = "select e.subdistrict_id,count(e.subdistrict_id) from TABLE1 e,TABLE2 s where e.subdistrict_id=s.id and e.subdistrict_id='"+ID+"' and e.flag='2' group by e.subdistrict_id"; 

                          //企业2 拼接查询条件 

                          String sql3 = "select e.subdistrict_id,count(e.subdistrict_id) from TABLE1 e,TABLE2 s where e.subdistrict_id=s.id and e.subdistrict_id='"+ID+"' and e.flag='1' group by e.subdistrict_id"; 

                          List<Map<String, Object>> list2 = operator.GetList(sql2); 

                          List<Map<String, Object>> list3 = operator.GetList(sql3); 

                          for (int i = 0; i < list2.size(); i++) { 

                              Map<String, Object> map2 = list2.get(i); 

                              String sum2 = map2.get("COUNT(E.SUBDISTRICT_ID)") == null ? "" : map2 

                                      .get("COUNT(E.SUBDISTRICT_ID)").toString(); 

                          for (int n = 0; n < list3.size(); n++) { 

                              Map<String, Object> map3 = list3.get(n); 

                              String sum3 = map3.get("COUNT(E.SUBDISTRICT_ID)") == null ? "" : map3 

                                  .get("COUNT(E.SUBDISTRICT_ID)").toString(); 

                   

                         // 1.document构建器   

                         Document doc = DocumentHelper.createDocument();   

                         // 2.添加元素(根)   

                         Element students = doc.addElement("chart"); 

                         // 一级 添加属性;   

                         students.addAttribute("caption","Country Comparison"); 

                         students.addAttribute("shownames","1"); 

                         students.addAttribute("showvalues","0"); 

                         students.addAttribute("showBorder","1"); 

                         students.addAttribute("decimals","0"); 

                         students.addAttribute("numberPrefix",""); 

                         //二级属性 
                            Element student1_2 = students.addElement("categories");
                        //三级属性
                             Element student1_3 = student1_2.addElement("category");   

                            student1_3.addAttribute("label",SUBDISTRICT_NAME);
                            /
                             Element student2_4 = students.addElement("dataset"); 

                             student2_4.addAttribute("seriesName","药械企业"); 

                             student2_4.addAttribute("color","00C78C"); 

                             student2_4.addAttribute("showValues","0"); 

                              

                             Element student2_5 = student2_4.addElement("set"); 

                             student2_5.addAttribute("value",sum3); 

                             

                             Element student1_4 = students.addElement("dataset"); 

                             student1_4.addAttribute("seriesName","餐饮企业"); 

                             student1_4.addAttribute("color","E3170D"); 

                             student1_4.addAttribute("showValues","0"); 

                              

                             Element student1_5 = student1_4.addElement("set"); 

                             student1_5.addAttribute("value",sum2); 

                   

                         //设置输出格式   

                         OutputFormat format=OutputFormat.createPrettyPrint();   

                         format.setEncoding("gbk"); 

                         //D:\apache-tomcat-6.0.29\webapps\项目名称服务器URL 

                         //D:\项目名称\WebRoot\ 本地URL 

                         //服务器使用URL 

                        // writer = new XMLWriter(new FileWriter(path+"/apache-tomcat-6.0.29/webapps/项目名称/XML文件/XXX.xml"),format);   

                         //本地测试URL -- 输出路径 

                         writer = new XMLWriter(new FileWriter("D:/项目名称/WebRoot/XML文件/XXX.xml"),format); 

                         writer.write(doc);   

                         System.out.println("生成XML成功!");   

                          } 

                      } 

                  } 

              } 

         } catch (Exception e) {   

             e.printStackTrace();   

         } finally {   

             try {   

                 writer.close();   

             } catch (IOException e) {   

                 // TODO Auto-generated catch block   

                 e.printStackTrace();   

             }   

         }   

     }   

 }