package com.whyc.dto;
|
|
|
import com.whyc.pojo.*;
|
import org.dom4j.Document;
|
import org.dom4j.DocumentException;
|
import org.dom4j.Element;
|
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();
|
Document document = reader.read(new File(xmlFilePath));
|
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.setBattDataList(battDataList);
|
fileInfo.getBattInfoList().add(battInfo);
|
}
|
fileInfo.setFileParam(fparam);
|
} catch (NullPointerException | NumberFormatException | DocumentException | ParseException e) {
|
res = false;
|
e.printStackTrace();
|
} finally {
|
if(false == res)
|
return null;
|
}
|
return fileInfo;
|
}
|
|
public static boolean writeXml(String xmlFilePath, Map<String,String> map){
|
boolean res=true;
|
try {
|
SAXReader reader = new SAXReader();
|
Document document = reader.read(new File(xmlFilePath));
|
//获取根元素
|
Element rootElement = document.getRootElement();
|
//获取根元素下的所有子元素
|
List<Element> list = rootElement.elements();
|
if(list!=null&&map!=null){
|
for (Element e:list) {
|
for (Map.Entry<String, String> entry:map.entrySet()) {
|
String mapKey = entry.getKey();
|
String mapValue = entry.getValue();
|
if(e.getName().equals(mapKey)){
|
e.setText(mapValue);
|
}
|
}
|
}
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
//format.setEncoding("UTF-8");//默认的编码就是UTF-8
|
XMLWriter writer = new XMLWriter( new FileOutputStream(xmlFilePath), format );
|
writer.write( document );
|
}else{
|
res=false;
|
}
|
} 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";
|
String filePath1="D://test.xml";
|
//FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
|
//System.out.println(fileInfo);
|
Map map=new HashMap();
|
map.put("test1","李军");
|
map.put("test2","张辉");
|
XmlFileOpreate.writeXml(filePath1,map);
|
}
|
}
|