File was renamed from src/main/java/com/whyc/dto/XmlFileRead.java |
| | |
| | | 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.File; |
| | | import java.io.*; |
| | | import java.text.ParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | |
| | | public class XmlFileRead{ |
| | | public class XmlFileOpreate { |
| | | |
| | | //解析xml文件 |
| | | public static void readConfigFromXml(String xmlFilePath) |
| | | public static FileInfo readXml(String xmlFilePath) |
| | | { |
| | | boolean res = true; |
| | | StationDto stationDto=new StationDto(); |
| | | FileParam fparam=stationDto.getFparam();//文件参数 |
| | | FileInfo fileInfo=stationDto.getFinfo();//文件信息 |
| | | |
| | | |
| | | FileInfo fileInfo=new FileInfo();//文件信息 |
| | | FileParam fparam=fileInfo.getFileParam();//文件参数 |
| | | try |
| | | { |
| | | SAXReader reader = new SAXReader(); |
| | |
| | | fparam.setEvaluationMode(Integer.parseInt(file_node.getTextTrim())); |
| | | |
| | | //查询出当前最大电池组id |
| | | System.out.println("groupName:"+fparam.getGroupNum()+" battCount:"+fparam.getBattCount()); |
| | | //System.out.println("groupName:"+fparam.getGroupNum()+" battCount:"+fparam.getBattCount()); |
| | | |
| | | //-----根据groupNum的值读取测试数据---------------------------// |
| | | Iterator nodes = rootnode.elementIterator("node_group_num"); |
| | |
| | | battInfo.setBattDataList(battDataList); |
| | | fileInfo.getBattInfoList().add(battInfo); |
| | | } |
| | | stationDto.setFparam(fparam); |
| | | stationDto.setFinfo(fileInfo); |
| | | System.out.println(stationDto); |
| | | fileInfo.setFileParam(fparam); |
| | | } catch (NullPointerException | NumberFormatException | DocumentException | ParseException e) { |
| | | res = false; |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if(false == res) |
| | | return ; |
| | | 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> es = rootElement.elements(); |
| | | for (Element e:es) { |
| | | 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 ); |
| | | } 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"; |
| | | XmlFileRead.readConfigFromXml(filePath); |
| | | 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); |
| | | } |
| | | } |