lxw
2022-09-19 98ac42a61e9821b8119ece243417729b311a68d6
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.whyc.dto;
 
 
import com.whyc.pojo.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
import java.io.*;
import java.text.ParseException;
import java.util.*;
 
public class XmlFileOpreate {
 
    //解析xml文件
    public static FileInfo readXml(String xmlFilePath)
    {
        boolean res = true;
        FileInfo fileInfo=new FileInfo();//文件信息
        FileParam fparam=fileInfo.getFileParam();//文件参数
        try
        {
            SAXReader reader = new SAXReader();
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
 
            Element node;//根节点
            Element file_node;//文件节点
            Element batt_node;//电池节点
 
            //------------------------------------------------------------//
            node = rootnode.element("TEST_TIME");
            String testTime=node.getTextTrim();
            fparam.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            //------------------------------------------------------------//
            node = rootnode.element("VERSION");
            fparam.setVersion(node.getTextTrim());
            //------------------------------------------------------------//
            node = rootnode.element("BATT_PARAM");
            file_node=node.element("uploadTime");
            String uploadTime=file_node.getTextTrim();
            if(!uploadTime.equals("null")){
                fparam.setUploadTime(ActionUtil.sdfwithALL.parse(uploadTime));
            }
            file_node=node.element("uploadflag");
            fparam.setUploadFlag(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("sysID");
            fparam.setSysId(file_node.getTextTrim());
            file_node=node.element("batt_group_name");
            fparam.setBattGroupName(file_node.getTextTrim());
            file_node=node.element("battBrand");
            fparam.setBattBrand(file_node.getTextTrim());
            file_node=node.element("battModel");
            fparam.setBattModel(file_node.getTextTrim());
            file_node=node.element("battBatch");
            fparam.setBattBatch(file_node.getTextTrim());
            file_node=node.element("battStation");
            fparam.setBattStation(file_node.getTextTrim());
            file_node=node.element("battlineName");
            fparam.setBattlineName(file_node.getTextTrim());
            file_node=node.element("battTHA");
            fparam.setBattTha(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("battErrFlag");
            fparam.setBattErrflag(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("battTestNum");
            fparam.setBattTestnum(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("battFaultNum");
            fparam.setBattFaultnum(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("batt_cap");
            fparam.setBattCap(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_count");
            fparam.setBattCount(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("batt_vol");
            fparam.setBattVol(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_res");
            fparam.setBattRes(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("gropNum");
            fparam.setGroupNum(Integer.parseInt(file_node.getTextTrim()));
 
            //------------------------------------------------------------//
            node = rootnode.element("TEST_PARAM");
            file_node=node.element("VolLowCoeK1");
            fparam.setVolLowCoeK1(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("VolHighCoeK2");
            fparam.setVolHighCoeK2(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("ResGoodCoeK3");
            fparam.setResGoodCoeK3(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("ResBadCoeK4");
            fparam.setResBadCoeK4(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("sample_percent");
            fparam.setSamplePercent(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("HighTempAlarm");
            fparam.setHighTempAlarm(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("ChainRes");
            fparam.setChainRes(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("evaluation_mode");
            fparam.setEvaluationMode(Integer.parseInt(file_node.getTextTrim()));
 
            //查询出当前最大电池组id
            //System.out.println("groupName:"+fparam.getGroupNum()+"   battCount:"+fparam.getBattCount());
 
            //-----根据groupNum的值读取测试数据---------------------------//
            Iterator nodes = rootnode.elementIterator("node_group_num");
            while (nodes.hasNext()){
                node= (Element) nodes.next();
                Iterator iterator1 = node.elementIterator();
                BattgroupInfo battInfo=new BattgroupInfo();
                List<BattgroupData> battDataList=new ArrayList();
                while (iterator1.hasNext()){
                    file_node= (Element) iterator1.next();
                    BattgroupData battData=new BattgroupData();
                    battData.setMonNum(Integer.parseInt(file_node.attributeValue("batt_num")));
                    batt_node=file_node.element("bv_1");
                    battData.setBv(Float.parseFloat(batt_node.getTextTrim()));
                    batt_node=file_node.element("br_1");
                    battData.setBr(Float.parseFloat(batt_node.getTextTrim()));
                    batt_node=file_node.element("bs_1");
                    battData.setBs(Float.parseFloat(batt_node.getTextTrim()));
                    batt_node=file_node.element("cr_1");
                    battData.setCr(Float.parseFloat(batt_node.getTextTrim()));
                    batt_node=file_node.element("er_1");
                    battData.setEr(Integer.parseInt(batt_node.getTextTrim()));
                    battDataList.add(battData);
                }
                battInfo.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
                battInfo.setBattDataList(battDataList);
                fileInfo.getBattInfoList().add(battInfo);
            }
            fileInfo.setFileParam(fparam);
            fiso.close();
        } catch (NullPointerException | NumberFormatException | DocumentException | ParseException | FileNotFoundException e) {
            res = false;
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(false == res)
                return null;
        }
        return  fileInfo;
    }
 
    public static boolean writeXml( Map<String,String> map,String xmlFilePath){
        boolean res=true;
        try {
            SAXReader reader = new SAXReader();
            FileInputStream fiso= new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
 
            for (Map.Entry<String, String> entry:map.entrySet()) {
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                System.out.println(mapKey+":"+mapValue);
                //得到第一个mapKey节点
                Node node=document.selectSingleNode("//"+mapKey);
                node.setText(mapValue);
            }
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");//默认的编码就是UTF-8
            XMLWriter writer = new XMLWriter( new FileOutputStream(xmlFilePath), format );
            writer.write( document );
            writer.close();
            fiso.close();
        } catch (DocumentException | FileNotFoundException | UnsupportedEncodingException e) {
            res = false;
            e.printStackTrace();
        } catch (IOException e) {
            res = false;
            e.printStackTrace();
        }
        return res;
    }
 
    public static void main(String[] args) {
        String filePath="D://2022-06-14 16-02-34.xml";
        Map map=new HashMap() ;
        //FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
        //System.out.println(fileInfo);
        map.put("ChainRes", "50.0");
        map.put("battTestNum","9");
        map.put("battModel","SD-001");
        map.put("batt_res","4.0");
        map.put("ResBadCoeK4","1.6");
        map.put("battlineName","一号线");
        map.put("evaluation_mode","0");
        map.put("TEST_TIME","2022-06-13 16:06:19");
        map.put("battErrFlag","1");
        map.put("gropNum","2");
        map.put("VolHighCoeK2","1.2");
        map.put("battBatch","20170602");
        map.put("batt_vol","12.0");
        map.put("batt_cap","100.0");
        map.put("sysID","B20220613160151");
        map.put("batt_count","24");
        map.put("uploadflag","0");
        map.put("uploadTime","null");
        map.put("battTHA","0");
        map.put("VolLowCoeK1","0.8");
        map.put("battFaultNum","2");
        map.put("HighTempAlarm","50");
        map.put("VERSION","V2.0");
        map.put("ResGoodCoeK3","1.25");
        map.put("sample_percent","30");
        map.put("VERSION","V2.0");
        map.put("battBrand","双登lxw");
        map.put("batt_group_name","后备电源室1#UPS系统lxw");
        map.put("battStation","鼓楼站lxw");
 
        XmlFileOpreate.writeXml(map,filePath);
    }
}