xmls

package com.monitor.resolve;

import java.io.File;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.monitor.entity.Fcs13;
import com.monitor.service.TimeFor;

@Service
public class ReadXmlService {

Log log = LogFactory.getLog(TimeFor.class);

@Autowired(required = false)
private JdbcTemplate jdbc;

public JdbcTemplate getJdbc() {
return jdbc;
}

public void setJdbc(JdbcTemplate jdbc) {
this.jdbc = jdbc;
}

private PreparedStatement ps=null;

Fcs13 book = new Fcs13();
public void resolve(){

refreshFileList("D:\\SAICFTEST\\INTERFACE\\SAICF_FCS\\FCS13_RFIDMONITORINFO\\");
}

private void refreshFileList(String strPath) {
String str = null;
String strctl = null;
    File dir = new File(strPath);
    File[] files = dir.listFiles();
    String fileName = null;
    String fileCtlName = null;
    List list = new ArrayList();
    List listctl = new ArrayList();
    if (files == null)
        return;
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            refreshFileList(files[i].getAbsolutePath());
        } else {
            String strFileName = files[i].getAbsolutePath().toLowerCase();
            String strFileNamedaxie = strFileName.toUpperCase();
            if(strFileName.endsWith(".xml")){
            str = strFileName;
              File str1 = new File(str.trim());
               fileName = str1.getName();
                   list.add(fileName);
            }             
        }
    }
   for(int j = 0;j<list.size();j++){
   File f1 = new File((String) list.get(j));
   try {
SAXReader reader = new SAXReader();
File f=new File("D:\\SAICFTEST\\INTERFACE\\SAICF_FCS\\FCS13_RFIDMONITORINFO\\"+f1);


Document doc = reader.read(f);
Element root=doc.getRootElement();

//取出所有的节点
List all = doc.selectNodes("//BODY/RECORD");
   for(Object obj:all){
   Node childNode =(Node) obj;
   List rfid=childNode.selectNodes("RFID");
   for(Object orfid:rfid){
   Node rf=(Node)orfid;
   book.setRfid(rf.getStringValue());
  
   }
  
   List occur_time = childNode.selectNodes("OCCUR_TIME");
   for(Object ooccur_time:occur_time){
   Node ot=(Node) ooccur_time;
   book.setOccur_Time(ot.getStringValue());
   }
  
   List monitor_flag = childNode.selectNodes("MONITOR_FLAG");
   for(Object omonitor_flag:monitor_flag){
   Node mf=(Node) omonitor_flag;
   book.setMonitor_Flag(mf.getStringValue());
   }
   try {
  
          String xmlname = f1.getName();
      
          String xmlnamesplit = xmlname.substring(0, 26);
         
          String XMLNAME = xmlnamesplit.toUpperCase();
         
         
       book.setFileName(XMLNAME);
      
   String sql = "insert into IF_GROUP_FCS13(FCS13_FileName,FCS13_Rfid,FCS13_OccurTime,FCS13_MonitorFlag,FCS13_RecStatus,FCS13_RecTime) values('"+book.getFileName()+"','"+book.getRfid()+"','"+book.getOccur_Time()+"','"+book.getMonitor_Flag()+"','0',getDate())";
  
   jdbc.execute(sql);
  
   String sql2 = "{call sp_if_gen_fcs12ctl('"+book.getFileName()+"')}";
  
   jdbc.execute(sql2);
     
} catch (Exception e) {
e.printStackTrace();
}

}
  
   File fold = new File("D:\\SAICFTEST\\INTERFACE\\SAICF_FCS\\FCS13_RFIDMONITORINFO\\"+f1);//生成XML文件路径
   String strNewPath = "D:\\SAICFTEST\\BACKUP\\SAICF_FCS\\FCS13_RFIDMONITORINFO\\";//移出XML文件路径
   File fnewpath = new File(strNewPath);
   if(!fnewpath.exists())
   fnewpath.mkdirs();
   File fnew = new File(strNewPath+fold.getName());
   fold.renameTo(fnew);
} catch (Exception e) {
   e.printStackTrace();
}
   }
  
//   for (int k = 0; k < files.length; k++) {
//        if (files[k].isDirectory()) {
//            refreshFileList(files[k].getAbsolutePath());
//        } else {
//           String strFileName1 = files[k].getAbsolutePath().toLowerCase();
//            if(strFileName1.endsWith(".ctl")){
//            strctl = strFileName1;
//             File strCtl = new File(strctl.trim());
//             fileCtlName = strCtl.getName();
//                  listctl.add(fileCtlName);
//            }
//        }
//    }
//  
//   for(int m = 0;m<listctl.size();m++){
//    File fctl = new File((String) listctl.get(m));
//    File foldctl = new File("D:\\SAICFTEST\\INTERFACE\\SAICF_FCS\\FCS13_RFIDMONITORINFO\\"+fctl);//生成的CTL路径
//    String strNewPathctl = "D:\\SAICFTEST\\BACKUP\\SAICF_FCS\\FCS13_RFIDMONITORINFO\\";//移出的CTL路径
//    File fnewpathctl = new File(strNewPathctl);
//    if(!fnewpathctl.exists())
//    fnewpathctl.mkdirs();
//    File fnewctl = new File(strNewPathctl+foldctl.getName());
//    foldctl.renameTo(fnewctl);   

}

}
}

你可能感兴趣的:(xml)