|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.BufferedWriter; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import javax.xml.parsers.DocumentBuilder; |
| 18 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 19 | +import javax.xml.stream.XMLOutputFactory; |
| 20 | +import javax.xml.stream.XMLStreamException; |
| 21 | +import javax.xml.stream.XMLStreamWriter; |
| 22 | + |
| 23 | +import org.w3c.dom.Document; |
| 24 | +import org.w3c.dom.NodeList; |
| 25 | + |
| 26 | +public class JimsimroDev { |
| 27 | + // Nombres de los archivos XML y JSON |
| 28 | + private final static String XML_FILENAME = "backendDeveloper.xml"; |
| 29 | + private final static String JSON_FILENAME = "backendDeveloper.json"; |
| 30 | + |
| 31 | + // Record para almacenar datos |
| 32 | + public static record MyTuple( |
| 33 | + String name, |
| 34 | + int age, |
| 35 | + LocalDate birthDate, |
| 36 | + List<String> programmingLanguages) { |
| 37 | + } |
| 38 | + |
| 39 | + // Clase para manejar los datos |
| 40 | + public static class Data { |
| 41 | + private String name; |
| 42 | + private int age; |
| 43 | + private LocalDate birthDate; |
| 44 | + List<String> programmingLanguages; |
| 45 | + |
| 46 | + public Data(String name, int age, LocalDate birthDate, List<String> programmingLanguages) { |
| 47 | + this.name = name; |
| 48 | + this.age = age; |
| 49 | + this.birthDate = birthDate; |
| 50 | + this.programmingLanguages = programmingLanguages; |
| 51 | + } |
| 52 | + |
| 53 | + public String getName() { |
| 54 | + return this.name; |
| 55 | + } |
| 56 | + |
| 57 | + public void setName(String name) { |
| 58 | + this.name = name; |
| 59 | + } |
| 60 | + |
| 61 | + public int getAge() { |
| 62 | + return this.age; |
| 63 | + } |
| 64 | + |
| 65 | + public void setAge(int age) { |
| 66 | + this.age = age; |
| 67 | + } |
| 68 | + |
| 69 | + public LocalDate getBirthDate() { |
| 70 | + return this.birthDate; |
| 71 | + } |
| 72 | + |
| 73 | + public void setBirthDate(LocalDate birthDate) { |
| 74 | + this.birthDate = birthDate; |
| 75 | + } |
| 76 | + |
| 77 | + public List<String> getProgrammingLanguages() { |
| 78 | + return this.programmingLanguages; |
| 79 | + } |
| 80 | + |
| 81 | + public void setProgrammingLanguages(List<String> programmingLanguages) { |
| 82 | + this.programmingLanguages = programmingLanguages; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Método para crear un archivo XML |
| 87 | + private static void createXML(MyTuple data, String xmlFilename) throws IOException, XMLStreamException { |
| 88 | + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); |
| 89 | + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileWriter(xmlFilename)); |
| 90 | + |
| 91 | + writer.writeStartDocument("UTF-8", "1.0"); |
| 92 | + writer.writeStartElement("data"); |
| 93 | + |
| 94 | + writer.writeStartElement("name"); |
| 95 | + writer.writeCharacters(data.name()); |
| 96 | + writer.writeEndElement(); |
| 97 | + |
| 98 | + writer.writeStartElement("age"); |
| 99 | + writer.writeCharacters(String.valueOf(data.age())); |
| 100 | + writer.writeEndElement(); |
| 101 | + |
| 102 | + writer.writeStartElement("birthDate"); |
| 103 | + writer.writeCharacters(data.birthDate().toString()); |
| 104 | + writer.writeEndElement(); |
| 105 | + |
| 106 | + writer.writeStartElement("programmingLanguages"); |
| 107 | + Iterator<String> it = data.programmingLanguages().iterator(); |
| 108 | + while (it.hasNext()) { |
| 109 | + String item = it.next(); |
| 110 | + writer.writeStartElement("item"); |
| 111 | + writer.writeCharacters(item); |
| 112 | + writer.writeEndElement(); |
| 113 | + } |
| 114 | + |
| 115 | + writer.writeEndElement(); |
| 116 | + |
| 117 | + writer.writeEndElement(); |
| 118 | + writer.writeEndDocument(); |
| 119 | + |
| 120 | + writer.flush(); |
| 121 | + writer.close(); |
| 122 | + System.out.println("XML file created successfully."); |
| 123 | + getFile(xmlFilename); |
| 124 | + // deleteFile(xmlFilename); |
| 125 | + } |
| 126 | + |
| 127 | + // Método para crear un archivo JSON |
| 128 | + private static void createJson(MyTuple data) throws IOException { |
| 129 | + File jsonFile = new File(JSON_FILENAME); |
| 130 | + |
| 131 | + StringBuilder json = new StringBuilder(); |
| 132 | + json.append("{\n"); |
| 133 | + json.append(" \"name\": \"" + data.name() + "\",\n"); |
| 134 | + json.append(" \"age\": " + data.age() + ",\n"); |
| 135 | + json.append(" \"birthDate\": \"" + data.birthDate() + "\",\n"); |
| 136 | + |
| 137 | + json.append(" \"programmingLanguages\": [\n"); |
| 138 | + for (int i = 0; i < data.programmingLanguages.size(); i++) { |
| 139 | + json.append(" \"" + data.programmingLanguages.get(i) + "\""); |
| 140 | + if (i < data.programmingLanguages.size() - 1) { |
| 141 | + json.append(","); |
| 142 | + } |
| 143 | + json.append("\n"); |
| 144 | + } |
| 145 | + json.append(" ]"); |
| 146 | + json.append("\n}"); |
| 147 | + |
| 148 | + System.out.println("JSON file created successfully."); |
| 149 | + |
| 150 | + // Escribir el archivo JSON |
| 151 | + try (BufferedWriter br = new BufferedWriter(new FileWriter(jsonFile))) { |
| 152 | + br.write(json.toString()); |
| 153 | + } |
| 154 | + getFile(JSON_FILENAME); |
| 155 | + // deleteFile(JSON_FILENAME); |
| 156 | + } |
| 157 | + |
| 158 | + // Método para leer un archivo y mostrar su contenido |
| 159 | + private static void getFile(String fileName) throws IOException, FileNotFoundException { |
| 160 | + FileReader file = new FileReader(fileName); |
| 161 | + try (BufferedReader bf = new BufferedReader(file)) { |
| 162 | + String line = ""; |
| 163 | + while ((line = bf.readLine()) != null) { |
| 164 | + System.out.println(line); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Método para parsear un JSON a un objeto Data |
| 170 | + private static Data parseJsonToData(String jsonString) { |
| 171 | + try { |
| 172 | + String name = jsonString.split("\"name\"\\s*:\\s*\"")[1].split("\"")[0]; |
| 173 | + int age = Integer.parseInt(jsonString.split("\"age\"\\s*:\\s*")[1].split(",")[0]); |
| 174 | + String birthDate = jsonString.split("\"birthDate\"\\s*:\\s*\"")[1].split("\"")[0]; |
| 175 | + String languagesString = jsonString.split("\"programmingLanguages\"\\s*:\\s*\\[")[1].split("\\]")[0]; |
| 176 | + List<String> programmingLanguages = new ArrayList<>(); |
| 177 | + for (String lang : languagesString.split(",")) { |
| 178 | + programmingLanguages.add(lang.replace("\"", "").trim()); |
| 179 | + } |
| 180 | + return new Data(name, age, LocalDate.parse(birthDate), programmingLanguages); |
| 181 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 182 | + System.err.println("Error parsing JSON: " + e.getMessage()); |
| 183 | + e.printStackTrace(); |
| 184 | + return null; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // Método para eliminar un archivo |
| 189 | + private static void deleteFile(String file) { |
| 190 | + Path path = Paths.get(file); |
| 191 | + try { |
| 192 | + Files.delete(path); |
| 193 | + } catch (IOException e) { |
| 194 | + System.out.println(e.getClass().getName() + " generated: " + e.getMessage()); |
| 195 | + e.printStackTrace(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // Método principal |
| 200 | + public static void main(String[] args) throws Exception, IOException, XMLStreamException { |
| 201 | + // Datos de ejemplo |
| 202 | + String name = "Jimmis Jhaon"; |
| 203 | + int age = 29; |
| 204 | + LocalDate birthDate = LocalDate.of(1995, 07, 28); |
| 205 | + String[] languages = { "Java", "Kotlin", "Elixir", "Lua" }; |
| 206 | + List<String> programmingLanguages = Arrays.asList(languages); |
| 207 | + |
| 208 | + MyTuple data = new MyTuple(name, age, birthDate, programmingLanguages); |
| 209 | + |
| 210 | + // Crear archivos XML y JSON |
| 211 | + createXML(data, XML_FILENAME); |
| 212 | + createJson(data); |
| 213 | + |
| 214 | + // Leer y parsear el archivo JSON |
| 215 | + FileReader file = new FileReader(JSON_FILENAME); |
| 216 | + try (BufferedReader bf = new BufferedReader(file)) { |
| 217 | + StringBuilder jsonBuilder = new StringBuilder(); |
| 218 | + String line = ""; |
| 219 | + while ((line = bf.readLine()) != null) { |
| 220 | + jsonBuilder.append(line); |
| 221 | + } |
| 222 | + String jsonString = jsonBuilder.toString(); |
| 223 | + Data jsonData = parseJsonToData(jsonString); |
| 224 | + System.out.println("Name: " + jsonData.getName()); |
| 225 | + System.out.println("Age: " + jsonData.getAge()); |
| 226 | + System.out.println("BirthDate: " + jsonData.getBirthDate()); |
| 227 | + System.out.println("Languages: " + jsonData.getProgrammingLanguages()); |
| 228 | + } |
| 229 | + |
| 230 | + // Leer y parsear el archivo XML |
| 231 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 232 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 233 | + Document document = builder.parse(new File(XML_FILENAME)); |
| 234 | + |
| 235 | + NodeList nodeList = document.getDocumentElement().getChildNodes(); |
| 236 | + String name1 = nodeList.item(0).getTextContent(); // Obtener el nombre del nodo |
| 237 | + int age1 = Integer.parseInt(nodeList.item(1).getTextContent()); // Obtener la edad del nodo |
| 238 | + LocalDate birthDate1 = LocalDate.parse(nodeList.item(2).getTextContent()); // Obtener la fecha de nacimiento del |
| 239 | + // nodo |
| 240 | + |
| 241 | + NodeList languages1 = nodeList.item(3).getChildNodes(); // Obtener los lenguajes de programación del nodo |
| 242 | + List<String> languagesList = new ArrayList<>(); // Convertir a lista |
| 243 | + for (int i = 0; i < languages1.getLength(); i++) { |
| 244 | + languagesList.add(languages1.item(i).getTextContent()); |
| 245 | + } |
| 246 | + |
| 247 | + Data xmlData = new Data(name1, age1, birthDate1, languagesList); |
| 248 | + |
| 249 | + System.out.println("Name: " + xmlData.getName()); |
| 250 | + System.out.println("Age: " + xmlData.getAge()); |
| 251 | + System.out.println("BirthDate: " + xmlData.getBirthDate()); |
| 252 | + System.out.println("Languages: " + String.join(", ", xmlData.getProgrammingLanguages())); |
| 253 | + |
| 254 | + // Eliminar archivos creados |
| 255 | + deleteFile(XML_FILENAME); |
| 256 | + deleteFile(JSON_FILENAME); |
| 257 | + } |
| 258 | +} |
0 commit comments