ParseJson.java
7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Copyright (c) 2020 Use is subject to license terms.
*/
package com.skua.common.util;
import com.alibaba.fastjson.JSONObject;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.UUIDGenerator;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* <pre>
* 数矿1.0 工艺监控配置oss上配置文件,模拟量解析工具类
* </pre>
* @author Li Yuanyuan
* @version V0.1, 2020年6月9日 下午3:13:38
*/
public class ParseJson {
public static void main(String[] args) throws Exception {
//parseXml();
//读取配置文件
File fileDir=new File("D:\\test");
File[] files = fileDir.listFiles();
for(File file : files) {
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
parseJson(sbf.toString(),file.getName());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
public static void parseJson(String value,String fileName) {
String id = fileName.substring(fileName.lastIndexOf("_")+1);
Map reprot = JSONObject.parseObject(value, Map.class);
List<Map<String,Object>> list = (List)reprot.get("copymsg");
for(Map<String,Object> map:list) {
String imgtitle = "";
String unit = "";
String imgprogramId = ConvertUtils.getString(map.get("imgprogramId"));
String imgcategory = ConvertUtils.getString(map.get("imgcategory"));
String imgtype = ConvertUtils.getString(map.get("imgtype"));
if(!StringUtils.isEmpty(imgprogramId)&&"zoneOne".equals(imgtype)) {
imgtitle = ConvertUtils.getString(map.get("imgtitle"));
Map<String,Object> cmap = (Map)map.get("configSet");
String temp = ConvertUtils.getString(cmap.get("moniAfterText"));
if(!StringUtils.isEmpty(temp)&&temp.contains("<")) {
int start = temp.indexOf(">")+1;
int end = temp.lastIndexOf("<");
unit = temp.substring(start,end);
}
System.out.println("insert into t_monitor_config(id,departId,monitor_manager_id,monitor_manager_name,program_id,program_name,program_unit,program_sort,upper,lower) values ('"+UUIDGenerator.creatUUID()+"','1151041418','"+id+"','"+imgcategory+"','"+imgprogramId+"','"+imgtitle+"','"+unit+"','','','');");
}
}
}
public static void parseXml() throws Exception {
SAXReader reader = new SAXReader();
StringReader sr = new StringReader("<DataTable xmlns=\"http://schemas.datacontract.org/2004/07/System.Data\"><xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table\" msdata:Locale=\"zh-CN\"><xs:complexType><xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"><xs:element name=\"Table\" msdata:CaseSensitive=\"False\" msdata:Locale=\"zh-CN\"><xs:complexType><xs:sequence><xs:element name=\"DeviceName\" type=\"xs:string\" minOccurs=\"0\" /><xs:element name=\"Communicate\" type=\"xs:string\" minOccurs=\"0\" /><xs:element name=\"DT\" type=\"xs:dateTime\" minOccurs=\"0\" /><xs:element name=\"Depth\" type=\"xs:decimal\" minOccurs=\"0\" /><xs:element name=\"AlarmName\" type=\"xs:string\" minOccurs=\"0\" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\"><DocumentElement xmlns=\"\"><Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"inserted\"><DeviceName>七一路地道桥</DeviceName><Communicate>0040280036</Communicate><DT>2020-06-11T15:14:16+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table2\" msdata:rowOrder=\"1\" diffgr:hasChanges=\"inserted\"><DeviceName>三丰路地道桥</DeviceName><Communicate>0040280050</Communicate><DT>2020-06-11T15:00:59+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table3\" msdata:rowOrder=\"2\" diffgr:hasChanges=\"inserted\"><DeviceName>八里庄地道桥</DeviceName><Communicate>0040280030</Communicate><DT>2020-06-11T15:00:59+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table4\" msdata:rowOrder=\"3\" diffgr:hasChanges=\"inserted\"><DeviceName>文明路(天威路)地道桥</DeviceName><Communicate>0040280027</Communicate><DT>2020-06-11T15:00:59+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table5\" msdata:rowOrder=\"4\" diffgr:hasChanges=\"inserted\"><DeviceName>朝阳路地道桥</DeviceName><Communicate>0040280045</Communicate><DT>2020-06-11T15:00:58+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table6\" msdata:rowOrder=\"5\" diffgr:hasChanges=\"inserted\"><DeviceName>新火车站</DeviceName><Communicate>0040280040</Communicate><DT>2020-06-11T15:00:58+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table7\" msdata:rowOrder=\"6\" diffgr:hasChanges=\"inserted\"><DeviceName>东风路地道桥</DeviceName><Communicate>0040280022</Communicate><DT>2020-06-11T15:00:57+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table8\" msdata:rowOrder=\"7\" diffgr:hasChanges=\"inserted\"><DeviceName>五四路地道桥</DeviceName><Communicate>0040280025</Communicate><DT>2020-06-11T15:00:01+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table9\" msdata:rowOrder=\"8\" diffgr:hasChanges=\"inserted\"><DeviceName>百花地道桥</DeviceName><Communicate>0040280043</Communicate><DT>2020-04-22T16:00:13+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table><Table diffgr:id=\"Table10\" msdata:rowOrder=\"9\" diffgr:hasChanges=\"inserted\"><DeviceName>环西(恒祥大街地道桥)</DeviceName><Communicate>0040280002</Communicate><DT>2019-12-19T15:00:00+08:00</DT><Depth>0.00</Depth><AlarmName>正常</AlarmName></Table></DocumentElement></diffgr:diffgram></DataTable>");
Document document = reader.read(sr);
//获取根节点
Element rootElement = document.getRootElement();
//子节点
List<Element> childElements = rootElement.elements();
//注意获取xml标签一定要一层一层的获取,或者可以采用xpath的方法,不过我这里试了xpath报错获取不到 ,最后还是一层一层的获取的,确实有点麻烦,不知道大佬们还有什么好的方法
Element newDataSet = childElements.get(1).element("DocumentElement");
Iterator iterator1 = newDataSet.elementIterator();
while (iterator1.hasNext()){
Element table = (Element) iterator1.next();
System.out.println(table.elementText("DeviceName"));
}
}
}