加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

以日期格式处理xml文件到json格式的txt文件

发布时间:2020-12-16 09:28:48 所属栏目:百科 来源:网络整理
导读:public static final SimpleDateFormat SDF_CHILD_PATH = new SimpleDateFormat("yyyyMMdd_HHmmssSSS"); public void messageProcess(String filePathName,String txtPathName,String identifier) { File file = new File(filePathName); String shopId="";

public static final SimpleDateFormat SDF_CHILD_PATH = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");

public void messageProcess(String filePathName,String txtPathName,String identifier) {
        File file = new File(filePathName);
        String shopId="";
        String shopStatus="";
        List<Map<String,Object>> shopInfoList = new ArrayList<Map<String,Object>>();
        File file2 = null;
        FileOutputStream out = null;
        BufferedOutputStream bf = null;
        int count = 0;
        List<File> fileslist = new ArrayList<File>();
    	List<File> listfiles = new ArrayList<File>();
    	sort(fileslist,file);
        try {
            SAXReader saxReader = new SAXReader();
            for (File shopFile : fileslist) {
                String fileName = shopFile.getName();
                String fileDate = StringUtils.split(fileName,"_")[0];

                // 删除前天的数据
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE,-2);
                if (fileDate.equals(SDF_TIME.format(new Timestamp(calendar.getTimeInMillis())))) {
                    FileUtils.deleteFile(shopFile.getPath());
                }
                // 解析今天的数据
                if (!fileDate.equals(SDF_TIME.format(new Date()))) {
                    continue;
                }
                logger.info("开始处理文件" + fileName);
                // System.out.println(shopFile.getPath());
                // 解析一个具体的文档
                sort(listfiles,shopFile);
                for (File file1 : listfiles) {
                    Reader reader = new InputStreamReader(new FileInputStream(file1),"UTF-8");
                    Document doc = saxReader.read(reader);
                    List<?> shopList = doc.selectNodes(identifier);
                    for (int i = 0; i < shopList.size(); i++) {
                        Map<String,Object> map = new HashMap<String,Object>();
                        Element shop = (Element) shopList.get(i);
                        for (Iterator<?> iter = shop.elementIterator(); iter.hasNext();) {
                            Element element = (Element) iter.next();
                            if("shopId".equals(element.getName())){
                            	shopId=element.getText();
                            	shopStatus=statusMap.get(shopId);
                            	if(null==shopStatus){
                            		shopStatus="0";
                            	}
                            }
                            if ("brandList".equals(element.getName())) {
                                List<Element> list = element.elements();
                                List<String> brandNameList = new ArrayList<String>();
                                for (Element e : list) {
                                    brandNameList.add(e.getText());
                                }
                                map.put("shopStatus",shopStatus);
                                map.put(list.get(0).getName(),brandNameList);
                            } else if ("categoryList".equals(element.getName())) {
                                List<?> list = element.selectNodes("categoryInfo");
                                List<String> categoryList = new ArrayList<String>();
                                for(int j = 0; j < list.size(); j++){
                                    Element category = (Element) list.get(j);
                                    for (Iterator<?> cateIter = category.elementIterator(); cateIter.hasNext();) {
                                        Element ele = (Element) cateIter.next();
                                        if("categoryName".equals(ele.getName())){
                                            categoryList.add(ele.getText());
                                        }
                                    }
                                }
                                if(statusMap.get(shopId)!=null){
                                	map.put("shopStatus",shopStatus);
                                }
                                map.put("category",categoryList);
                            } else {
                                map.put(element.getName(),element.getText());
                            }
                        }
                        shopInfoList.add(map);
                        count++;
                        if (count == SystemArgument.SHOP_COUNT) {
                            file2 = new File(txtPathName + File.separator
                                    + SystemArgument.SDF_CHILD_PATH.format(new Date()) + ".txt");
                            if (!file2.exists()) {
                                boolean isExit = file2.createNewFile();
                                if (isExit == true) {
                                    out = new FileOutputStream(file2);
                                    bf = new BufferedOutputStream(out);
                                } else {
                                    logger.error("在" + file2.getName() + "创建文件的时候失败");
                                }
                            } else {
                                out = new FileOutputStream(file2);
                                bf = new BufferedOutputStream(out);
                            }
                            JSONArray ja = JSONArray.fromObject(shopInfoList);
                            bf.write(ja.toString().concat("n").getBytes("UTF-8"));
                            bf.flush();
                            count = 0;
                            shopList.clear();
                        }
                    }
                }
                logger.info("文件" + fileName + "处理结束!");
            }
            if (!shopInfoList.isEmpty()) {
                file2 = new File(txtPathName + File.separator + SystemArgument.SDF_CHILD_PATH.format(new Date())
                        + ".txt");
                if (!file2.exists()) {
                    boolean isExit = file2.createNewFile();
                    if (isExit == true) {
                        out = new FileOutputStream(file2);
                        bf = new BufferedOutputStream(out);
                    } else {
                        logger.error("在" + file2.getName() + "创建文件的时候失败");
                    }
                } else {
                    out = new FileOutputStream(file2);
                    bf = new BufferedOutputStream(out);
                }
                JSONArray ja = JSONArray.fromObject(shopInfoList);
                bf.write(ja.toString().concat("n").getBytes("UTF-8"));
                bf.flush();
            }
        } catch (DocumentException e) {
            logger.error(e.getMessage(),e);
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(),e);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(),e);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读