/
Shcod
/
java-core-Parser
Обзор
Документация
Войти
/
Shcod
/
java-core-Parser
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/service/Parser.java
121 строка
4 KB
Shcod
first commit
10 авг 2026, 01:10
10 авг 2026, 01:10
4aa8947
Код
Авторство
О чём код?
package service; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.opencsv.CSVReader; import com.opencsv.bean.ColumnPositionMappingStrategy; import com.opencsv.bean.CsvToBean; import com.opencsv.bean.CsvToBeanBuilder; import entity.Employee; import org.json.simple.JSONArray; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class Parser { public List<Employee> parseCSV(String[] columnMapping, String filename) { List<Employee> employeeList; try (CSVReader reader = new CSVReader(new FileReader(filename))) { ColumnPositionMappingStrategy<Employee> strategy = new ColumnPositionMappingStrategy<>(); strategy.setType(Employee.class); strategy.setColumnMapping(columnMapping); CsvToBean<Employee> csv = new CsvToBeanBuilder<Employee>(reader).withMappingStrategy(strategy).build(); employeeList = csv.parse(); } catch (IOException e) { throw new RuntimeException(e); } return employeeList; } public List<Employee> parseXML(String filename) { List<Employee> employeeList = new ArrayList<>(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File(filename)); Node root = doc.getDocumentElement(); NodeList nodeList = root.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; long id = Long.parseLong(element.getElementsByTagName("id").item(0).getTextContent()); String firstName = element.getElementsByTagName("firstName").item(0).getTextContent(); String lastName = element.getElementsByTagName("lastName").item(0).getTextContent(); String country = element.getElementsByTagName("country").item(0).getTextContent(); int age = Integer.parseInt(element.getElementsByTagName("age").item(0).getTextContent()); employeeList.add(new Employee(id, firstName, lastName, country, age)); } } } catch (ParserConfigurationException | SAXException | IOException e) { throw new RuntimeException(e); } return employeeList; } public String listToJson(List<Employee> employees) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Type listType = new TypeToken<List<Employee>>() { }.getType(); return gson.toJson(employees, listType); } public List<Employee> jsonToList(String path) { List<Employee> result = new ArrayList<>(); JSONParser parser = new JSONParser(); try { JSONArray arr = (JSONArray) parser.parse(readString(path)); GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); for (Object o : arr) { result.add(gson.fromJson(o.toString(), Employee.class)); } } catch (ParseException e) { throw new RuntimeException(e); } return result; } public String readString(String path) { StringBuilder sb = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(path))) { String line; while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { throw new RuntimeException(e); } return sb.toString(); } public void writeString(String path, String json) { try (FileWriter writer = new FileWriter(path)) { writer.write(json); writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } } }