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

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

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

  1. public void messageProcess(String filePathName,String txtPathName,String identifier) {
  2. File file = new File(filePathName);
  3. String shopId="";
  4. String shopStatus="";
  5. List<Map<String,Object>> shopInfoList = new ArrayList<Map<String,Object>>();
  6. File file2 = null;
  7. FileOutputStream out = null;
  8. BufferedOutputStream bf = null;
  9. int count = 0;
  10. List<File> fileslist = new ArrayList<File>();
  11. List<File> listfiles = new ArrayList<File>();
  12. sort(fileslist,file);
  13. try {
  14. SAXReader saxReader = new SAXReader();
  15. for (File shopFile : fileslist) {
  16. String fileName = shopFile.getName();
  17. String fileDate = StringUtils.split(fileName,"_")[0];
  18.  
  19. // 删除前天的数据
  20. Calendar calendar = Calendar.getInstance();
  21. calendar.add(Calendar.DATE,-2);
  22. if (fileDate.equals(SDF_TIME.format(new Timestamp(calendar.getTimeInMillis())))) {
  23. FileUtils.deleteFile(shopFile.getPath());
  24. }
  25. // 解析今天的数据
  26. if (!fileDate.equals(SDF_TIME.format(new Date()))) {
  27. continue;
  28. }
  29. logger.info("开始处理文件" + fileName);
  30. // System.out.println(shopFile.getPath());
  31. // 解析一个具体的文档
  32. sort(listfiles,shopFile);
  33. for (File file1 : listfiles) {
  34. Reader reader = new InputStreamReader(new FileInputStream(file1),"UTF-8");
  35. Document doc = saxReader.read(reader);
  36. List<?> shopList = doc.selectNodes(identifier);
  37. for (int i = 0; i < shopList.size(); i++) {
  38. Map<String,Object> map = new HashMap<String,Object>();
  39. Element shop = (Element) shopList.get(i);
  40. for (Iterator<?> iter = shop.elementIterator(); iter.hasNext();) {
  41. Element element = (Element) iter.next();
  42. if("shopId".equals(element.getName())){
  43. shopId=element.getText();
  44. shopStatus=statusMap.get(shopId);
  45. if(null==shopStatus){
  46. shopStatus="0";
  47. }
  48. }
  49. if ("brandList".equals(element.getName())) {
  50. List<Element> list = element.elements();
  51. List<String> brandNameList = new ArrayList<String>();
  52. for (Element e : list) {
  53. brandNameList.add(e.getText());
  54. }
  55. map.put("shopStatus",shopStatus);
  56. map.put(list.get(0).getName(),brandNameList);
  57. } else if ("categoryList".equals(element.getName())) {
  58. List<?> list = element.selectNodes("categoryInfo");
  59. List<String> categoryList = new ArrayList<String>();
  60. for(int j = 0; j < list.size(); j++){
  61. Element category = (Element) list.get(j);
  62. for (Iterator<?> cateIter = category.elementIterator(); cateIter.hasNext();) {
  63. Element ele = (Element) cateIter.next();
  64. if("categoryName".equals(ele.getName())){
  65. categoryList.add(ele.getText());
  66. }
  67. }
  68. }
  69. if(statusMap.get(shopId)!=null){
  70. map.put("shopStatus",shopStatus);
  71. }
  72. map.put("category",categoryList);
  73. } else {
  74. map.put(element.getName(),element.getText());
  75. }
  76. }
  77. shopInfoList.add(map);
  78. count++;
  79. if (count == SystemArgument.SHOP_COUNT) {
  80. file2 = new File(txtPathName + File.separator
  81. + SystemArgument.SDF_CHILD_PATH.format(new Date()) + ".txt");
  82. if (!file2.exists()) {
  83. boolean isExit = file2.createNewFile();
  84. if (isExit == true) {
  85. out = new FileOutputStream(file2);
  86. bf = new BufferedOutputStream(out);
  87. } else {
  88. logger.error("在" + file2.getName() + "创建文件的时候失败");
  89. }
  90. } else {
  91. out = new FileOutputStream(file2);
  92. bf = new BufferedOutputStream(out);
  93. }
  94. JSONArray ja = JSONArray.fromObject(shopInfoList);
  95. bf.write(ja.toString().concat("\n").getBytes("UTF-8"));
  96. bf.flush();
  97. count = 0;
  98. shopList.clear();
  99. }
  100. }
  101. }
  102. logger.info("文件" + fileName + "处理结束!");
  103. }
  104. if (!shopInfoList.isEmpty()) {
  105. file2 = new File(txtPathName + File.separator + SystemArgument.SDF_CHILD_PATH.format(new Date())
  106. + ".txt");
  107. if (!file2.exists()) {
  108. boolean isExit = file2.createNewFile();
  109. if (isExit == true) {
  110. out = new FileOutputStream(file2);
  111. bf = new BufferedOutputStream(out);
  112. } else {
  113. logger.error("在" + file2.getName() + "创建文件的时候失败");
  114. }
  115. } else {
  116. out = new FileOutputStream(file2);
  117. bf = new BufferedOutputStream(out);
  118. }
  119. JSONArray ja = JSONArray.fromObject(shopInfoList);
  120. bf.write(ja.toString().concat("\n").getBytes("UTF-8"));
  121. bf.flush();
  122. }
  123. } catch (DocumentException e) {
  124. logger.error(e.getMessage(),e);
  125. } catch (UnsupportedEncodingException e) {
  126. logger.error(e.getMessage(),e);
  127. } catch (FileNotFoundException e) {
  128. logger.error(e.getMessage(),e);
  129. } catch (IOException e) {
  130. logger.error(e.getMessage(),e);
  131. }
  132. }

猜你在找的XML相关文章