lxw
2022-09-16 547b6ac144d0745d9b55526c062b2fcaeb80fe4e
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
package com.whyc.service;
 
import com.whyc.dto.ActionUtil;
import com.whyc.dto.FileParamToXml;
import com.whyc.dto.Response;
import com.whyc.dto.XmlFileOpreate;
import com.whyc.pojo.FileInfo;
import com.whyc.pojo.FileParam;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
 
@Service
public class FileParamService {
 
    //解析xml文件
    public Response getXmlValue(String filePath) {
        File file=new File(filePath);
        if(file.exists()){
            FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
            return new Response().setII(1,fileInfo!=null,fileInfo,"返回解析数据");
        }else{
            return new Response().set(1,false,"文件路径不正确");
        }
    }
 
 
    //通过修改属性窗口值来修改文件值
    public Response updateXmlByFileParam(FileParam fileParam,String filePath) {
        Map<String,String> map=new HashMap<>();
        Class paramClass=fileParam.getClass();
        // 获取所有的属性数组
        Field[] fields = paramClass.getDeclaredFields();
        for (Field field:fields) {
            field.setAccessible(true);
            try {
                String paramName=field.getName();
                //获取属性值
                Object obj=field.get(fileParam);
                if(obj==null){
                    obj="null";
                }
                String paramValue=obj.toString();
                if(field.getType().toString().equals("class java.util.Date")){
                    if(!paramValue.equals("null")){
                        paramValue= ActionUtil.sdfwithALL.format(ActionUtil.df.parse(paramValue));
                    }
                }
                String xmlName=FileParamToXml.getNameByType(paramName);
                if(xmlName!=null&&!xmlName.isEmpty()){
                    map.put(xmlName,paramValue);
                }
            } catch (IllegalAccessException | ParseException e) {
                e.printStackTrace();
            }
        }
        boolean bl=false;
        if(map.size()>0){
            bl=XmlFileOpreate.writeXml(map,filePath);
        }
        return new Response().setII(1,bl,map,"修改文件");
    }
    //通过修改属性窗口值来修改文件值
    public Response updateXmlByParamMap(Map<String,String> map,String filePath) {
        boolean bl=false;
        if(map.size()>0){
            bl=XmlFileOpreate.writeXml(map,filePath);
        }
        return new Response().setII(1,bl,map,"修改文件");
    }
}