Skip to content

Commit e66e60e

Browse files
authored
Merge pull request mouredev#2449 from Cesar-Ch/add-cesar-roadmap
#12 - Javascript
2 parents f875532 + 0c02e2b commit e66e60e

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* #12 JSON Y XML
3+
*/
4+
5+
const fs = require('fs').promises;
6+
7+
// Datos a guardar
8+
const datos = {
9+
nombre: 'cesar-ch',
10+
edad: 3,
11+
fechaNacimiento: '2021-02-03',
12+
lenguajes: ['JavaScript', 'Python', 'Java']
13+
};
14+
15+
async function main() {
16+
// Guardar en archivo XML
17+
const datosXML = `<?xml version="1.0" encoding="UTF-8"?>
18+
<datos>
19+
<nombre>${datos.nombre}</nombre>
20+
<edad>${datos.edad}</edad>
21+
<fechaNacimiento>${datos.fechaNacimiento}</fechaNacimiento>
22+
<lenguajes>${datos.lenguajes.join(',')}</lenguajes>
23+
</datos>`;
24+
await fs.writeFile('datos.xml', datosXML);
25+
console.log('Archivo XML creado');
26+
27+
// Guardar en archivo JSON
28+
const datosJSON = JSON.stringify(datos, null, 2);
29+
await fs.writeFile('datos.json', datosJSON);
30+
console.log('Archivo JSON creado');
31+
32+
/*
33+
* DIFICULTAD EXTRA
34+
*/
35+
36+
// Leer archivo XML
37+
const xmlData = await fs.readFile('datos.xml', 'utf8');
38+
console.log('Contenido del archivo XML:', xmlData);
39+
40+
// Leer archivo JSON
41+
const jsonData = await fs.readFile('datos.json', 'utf8');
42+
console.log('Contenido del archivo JSON:', jsonData);
43+
44+
// Borrar archivo XML
45+
await fs.unlink('datos.xml');
46+
console.log('Archivo XML eliminado');
47+
48+
// Borrar archivo JSON
49+
await fs.unlink('datos.json');
50+
console.log('Archivo JSON eliminado');
51+
}
52+
main();

0 commit comments

Comments
 (0)