whyclxw
2025-03-31 b46f7fa6d77852ae9e28a062da234a2ddf9405fb
src/main/java/com/whyc/dto/XmlFileOpreate.java
@@ -2,9 +2,11 @@
import com.whyc.pojo.*;
import com.whyc.util.MathUtil;
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;
@@ -14,17 +16,213 @@
import java.util.*;
public class XmlFileOpreate {
    //解析FBR-6000NT基础2.7
    public static FileInfo readFBR_6000NT_jc(String xmlFilePath){
        FileInfo fileInfo=new FileInfo();//文件信息
        FileParam fparam=fileInfo.getFileParam();//文件参数
        boolean res = true;
        try
        {
            SAXReader reader = new SAXReader();
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
    //解析xml文件
    public static FileInfo readXml(String xmlFilePath)
    {
            Element node;//根节点
            Element file_node;//文件节点
            //------------------------------------------------------------//
            node = rootnode.element("TEST_TIME");
            String testTime=node.getTextTrim();
            testTime=testTime.replace("_"," ");
            fparam.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            //------------------------------------------------------------//
            node = rootnode.element("VERSION");
            fparam.setVersion(node.getTextTrim());
            //------------------------------------------------------------//
            node = rootnode.element("BATT_PARAM");
            file_node=node.element("batt_group_name");
            fparam.setBattGroupName(file_node.getTextTrim());
            file_node=node.element("batt_producer");
            if(file_node!=null){
                fparam.setBattProducer(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_vol_good");
            if(file_node!=null){
                fparam.setBattVolGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_vol_bad");
            if(file_node!=null){
                fparam.setBattVolBad(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_res");
            if(file_node!=null){
                fparam.setBattRes(Float.parseFloat(file_node.getTextTrim()));
            }else{
                fparam.setBattRes(0f);
            }
            file_node=node.element("batt_res_good");
            if(file_node!=null){
                fparam.setBattResGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_res_bad");
            if(file_node!=null){
                fparam.setBattResBad(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_ser");
            fparam.setBattSer(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_ser_good");
            if(file_node!=null){
                fparam.setBattSerGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_ser_bad");
            if(file_node!=null){
                fparam.setBattSerBad(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("concount_pb");
            fparam.setConcountPb(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("conn_res");
            if(file_node!=null){
                fparam.setConnRes(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("conn_res_good");
            if(file_node!=null){
                fparam.setConnResGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("conn_res_bad");
            if(file_node!=null){
                fparam.setConnResBad(Float.parseFloat(file_node.getTextTrim()));
            }
            float battCap=fparam.getBattCap();//标称容量
            float battRes=fparam.getBattRes();//标称内阻
            //-----根据groupNum的值读取测试数据---------------------------//
            Iterator nodes = rootnode.elementIterator("node_batt_num");
            BattgroupInfo battInfo=new BattgroupInfo();
            List<BattgroupData> battDataList=new ArrayList();
            //List<Float> list=new ArrayList();//存放所有的内阻数据
            List<Float> avglist=new ArrayList();//存放有效的内阻数据用于计算偏差率(当标称内阻为0时,用平内阻)
            int avgNum=0;
            if(nodes!=null){
                while (nodes.hasNext()){
                    node= (Element) nodes.next();
                    BattgroupData battData=new BattgroupData();
                    battData.setMonNum(Integer.parseInt(node.attributeValue("batt_num")));
                    file_node=node.element("bv_1");
                    battData.setBv(file_node.getTextTrim());
                    file_node=node.element("br_1");
                    String br=file_node.getTextTrim();
                    battData.setBr(br);
                    /*float esCap=0f;
                    if(battRes==0){
                        esCap=1;
                    }else{
                        esCap=battRes/Float.valueOf(br);
                    }
                    battData.setEstimatedCap(esCap*battCap);*/
                    /* float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(br),battRes);
                    //容量百分比修改
                    battData.setPreCapPercent(preCapPercent);
                    //预估容量
                    battData.setEstimatedCap(preCapPercent*battCap);
                   //内阻偏差率设置默认值为0
                    if(battRes==0){
                        battData.setResDevRate(Float.POSITIVE_INFINITY);
                    }else{
                        float resDevRate=(Float.valueOf(br)-battRes)/battRes;
                        battData.setResDevRate(resDevRate);
                    }*/
                    file_node=node.element("bs_1");
                    battData.setBs(file_node.getTextTrim());
                    file_node=node.element("cr_1");
                    battData.setCr(file_node.getTextTrim());
                    battDataList.add(battData);
                    //list.add(Float.valueOf(battData.getBr()));
                    //获取有效内阻
                    if(Float.parseFloat(battData.getBv())>0&&Float.parseFloat(battData.getBr())>0){
                        avgNum++;
                        avglist.add(Float.parseFloat(battData.getBr()));
                    }
                }
            }
            battInfo.setBattDataList(battDataList);
            battInfo.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            fileInfo.getBattInfoList().add(battInfo);
            //当标称内阻为0时,计算最低三分之一单体数内阻数据的平均值
            if(fparam.getBattRes()==0){
                /*if(list!=null&&list.size()>0){
                    int battNum= (int) Math.ceil(list.size()/3);
                    float resSum=0f;
                    Collections.sort(list);
                    for(int i=0;i<battNum;i++){
                        resSum=resSum+list.get(i);
                    }
                    String avgRes=String.format("%.2f",resSum/battNum);
                    fparam.setBattRes(Float.valueOf(avgRes));
                }*/
                //计算平局内阻
                if(avglist!=null&&avglist.size()>0){
                    Collections.sort(avglist);
                    System.err.println(avglist.toString());
                    float sunBr=0f;
                    avgNum= (int) Math.ceil(0.01*avgNum*(100-fparam.getSamplePercent()));
                    int effNum=0;
                    for (int i=0;i<avglist.size();i++) {
                        if(i!=0&&i<=avgNum){
                            sunBr+=avglist.get(i);
                            effNum++;
                        }
                    }
                    String avgRes=String.format("%.2f",sunBr/effNum);
                    fparam.setBattRes(Float.valueOf(avgRes));
                }
            }
            float effRes=fparam.getBattRes();  //有效的标称内阻
            for (BattgroupData battData:battInfo.getBattDataList()) {
                float resDevRate=(Float.valueOf(battData.getBr())-effRes)/effRes;
                battData.setResDevRate(resDevRate);
                float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(battData.getBr()),effRes);
                //容量百分比修改
                battData.setPreCapPercent(preCapPercent);
                //预估容量
                battData.setEstimatedCap(preCapPercent*battCap);
            }
            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;
    }
    //解析FBR-6000NT带标记版本2.0
    public static FileInfo readFBR_6000NT_bj(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));
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
            Element node;//根节点
@@ -34,6 +232,7 @@
            //------------------------------------------------------------//
            node = rootnode.element("TEST_TIME");
            String testTime=node.getTextTrim();
            testTime.replace("_"," ");
            fparam.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            //------------------------------------------------------------//
            node = rootnode.element("VERSION");
@@ -76,7 +275,12 @@
            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()));
            if(file_node!=null){
                fparam.setBattRes(Float.parseFloat(file_node.getTextTrim()));
            }else{
                fparam.setBattRes(0f);
            }
            file_node=node.element("gropNum");
            fparam.setGroupNum(Integer.parseInt(file_node.getTextTrim()));
@@ -104,33 +308,114 @@
            //-----根据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);
            float battCap=fparam.getBattCap();//标称容量
            float battRes=fparam.getBattRes();//标称内阻
            if(nodes!=null){
                while (nodes.hasNext()){
                    node= (Element) nodes.next();
                    Iterator iterator1 = node.elementIterator();
                    BattgroupInfo battInfo=new BattgroupInfo();
                    List<BattgroupData> battDataList=new ArrayList();
                    //List<Float> list=new ArrayList();//存放所有的内阻数据
                    List<Float> avglist=new ArrayList();//存放有效的内阻数据用于计算偏差率(当标称内阻为0时,用平内阻)
                    int avgNum=0;
                    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(batt_node.getTextTrim());
                        batt_node=file_node.element("br_1");
                        String br=batt_node.getTextTrim();
                        battData.setBr(br);
                       /* float esCap=0f;
                        if(battRes==0){
                            esCap=1;
                        }else{
                            esCap=battRes/Float.valueOf(br);
                        }
                        battData.setEstimatedCap(esCap*battCap);*/
                        /*float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(br),battRes);
                        //容量百分比修改
                        battData.setPreCapPercent(preCapPercent);
                        //预估容量
                        battData.setEstimatedCap(preCapPercent*battCap);
                        //内阻偏差率
                        if(battRes==0){
                            battData.setResDevRate(Float.POSITIVE_INFINITY);
                        }else{
                            float resDevRate=(Float.valueOf(br)-battRes)/battRes;
                            battData.setResDevRate(resDevRate);
                        }*/
                        batt_node=file_node.element("bs_1");
                        battData.setBs(batt_node.getTextTrim());
                        batt_node=file_node.element("cr_1");
                        battData.setCr(batt_node.getTextTrim());
                        batt_node=file_node.element("er_1");
                        battData.setEr(Integer.parseInt(batt_node.getTextTrim()));
                        battDataList.add(battData);
                        //list.add(Float.valueOf(battData.getBr()));
                        //获取有效内阻
                        if(Float.parseFloat(battData.getBv())>0&&Float.parseFloat(battData.getBr())>0){
                            avgNum++;
                            avglist.add(Float.parseFloat(battData.getBr()));
                        }
                    }
                    battInfo.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
                    battInfo.setBattDataList(battDataList);
                    fileInfo.getBattInfoList().add(battInfo);
                    //当标称内阻为0时,计算最低三分之一单体数内阻数据的平均值
                    /*if(fparam.getBattRes()==0){
                        if(list!=null&&list.size()>0){
                            int battNum= (int) Math.ceil(list.size()/3);
                            float resSum=0f;
                            Collections.sort(list);
                            for(int i=0;i<battNum;i++){
                                resSum=resSum+list.get(i);
                            }
                            String avgRes=String.format("%.2f",resSum/battNum);
                            fparam.setBattRes(Float.valueOf(avgRes));
                        }
                    }*/
                    if(fparam.getBattRes()==0){
                        //计算平局内阻
                        if(avglist!=null&&avglist.size()>0){
                            Collections.sort(avglist);
                            System.err.println(avglist.toString());
                            float sunBr=0f;
                            avgNum= (int) Math.ceil(0.01*avgNum*(100-fparam.getSamplePercent()));
                            int effNum=0;
                            for (int i=0;i<avglist.size();i++) {
                                if(i!=0&&i<=avgNum){
                                    sunBr+=avglist.get(i);
                                    effNum++;
                                }
                            }
                            String avgRes=String.format("%.2f",sunBr/effNum);
                            fparam.setBattRes(Float.valueOf(avgRes));
                        }
                    }
                    float effRes=fparam.getBattRes();  //有效的标称内阻
                    for (BattgroupData battData:battInfo.getBattDataList()) {
                        float resDevRate=(Float.valueOf(battData.getBr())-effRes)/effRes;
                        battData.setResDevRate(resDevRate);
                        float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(battData.getBr()),effRes);
                        //容量百分比修改
                        battData.setPreCapPercent(preCapPercent);
                        //预估容量
                        battData.setEstimatedCap(preCapPercent*battCap);
                    }
                }
                battInfo.setBattDataList(battDataList);
                fileInfo.getBattInfoList().add(battInfo);
            }
            fileInfo.setFileParam(fparam);
        } catch (NullPointerException | NumberFormatException | DocumentException | ParseException e) {
            fiso.close();
        } catch (NullPointerException | NumberFormatException | DocumentException | ParseException | FileNotFoundException e) {
            res = false;
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(false == res)
@@ -139,32 +424,60 @@
        return  fileInfo;
    }
    public static boolean writeXml(String xmlFilePath, Map<String,String> map){
    //解析xml文件
    public static FileInfo readXml(String xmlFilePath)
    {
        FileInfo fileInfo=new FileInfo();
        try
        {
            SAXReader reader = new SAXReader();
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
            //------------------------------------------------------------//
            Element node = rootnode.element("VERSION");
            if(node!=null){
                String version=node.getTextTrim();
                switch (version){
                    case "V2.0": fileInfo=readFBR_6000NT_bj(xmlFilePath);break;
                    case "V2.7": fileInfo=readFBR_6000NT_jc(xmlFilePath);break;
                    default:fileInfo=readFBR_6000NT_bj(xmlFilePath);break;
                }
            }
            fiso.close();
        } catch (NullPointerException | NumberFormatException | DocumentException  | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  fileInfo;
    }
    public static boolean writeXml( Map<String,String> map,String xmlFilePath){
        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);
                        }
                    }
            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);
                if(node!=null){
                    node.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;
            }
            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();
@@ -176,13 +489,40 @@
    }
    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);
        String filePath="D:\\fileTest\\FBR-5000CT -原数据20221031\\\\2022-10-31_09-14-48.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);
    }
}