whycxzp
2022-10-26 ada3e9094ecfb8b995c5580bbb59a1d64d89a801
src/main/java/com/whyc/dto/XmlFileOpreate.java
@@ -5,6 +5,7 @@
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;
@@ -24,7 +25,8 @@
        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;//根节点
@@ -125,12 +127,16 @@
                    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);
        } 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 +145,27 @@
        return  fileInfo;
    }
    public static boolean writeXml(String xmlFilePath, Map<String,String> map){
    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);
                        }
                    }
                }
                OutputFormat format = OutputFormat.createPrettyPrint();
                //format.setEncoding("UTF-8");//默认的编码就是UTF-8
                XMLWriter writer = new XMLWriter( new FileOutputStream(xmlFilePath), format );
                writer.write( document );
            }else{
                res=false;
            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();
@@ -177,12 +178,39 @@
    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","lxw");
        map.put("test2","hw");
        //XmlFileOpreate.writeXml(filePath1,map);
        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);
    }
}