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

前端之家收集整理的这篇文章主要介绍了以日期格式处理xml文件到json格式的txt文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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);
        }
    }
原文链接:https://www.f2er.com/xml/297786.html

猜你在找的XML相关文章