Skip to content

Commit 595d22a

Browse files
committed
#12 - TypeScript
1 parent 8351724 commit 595d22a

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// 1º Ejercicio
2+
let fs = require('fs')
3+
let readlineSync = require('readline-sync');
4+
let { DOMImplementation: ImplementationDOM, XMLSerializer: SerializeXML } = require('xmldom');
5+
6+
// Para poder crear el archivo
7+
8+
interface Contenido {
9+
nombre: string,
10+
edad: number,
11+
fecha_nacimiento: string,
12+
lenguajes: Array<string>
13+
}
14+
15+
let json: Contenido = {
16+
nombre: 'Adrián Iglesias',
17+
edad: 19,
18+
fecha_nacimiento: '03/11/2004',
19+
lenguajes: ['TypeScript', 'JavaScript', 'HTML', 'CSS']
20+
}
21+
22+
fs.writeFile(
23+
'info.json',
24+
JSON.stringify(json),
25+
(error: NodeJS.ErrnoException | null) => {
26+
if(error) throw error
27+
console.log('JSON creado!')
28+
})
29+
30+
//Leer contenido del fichero JSON
31+
fs.readFile('info.json', (error: NodeJS.ErrnoException | null, data: any) => {
32+
if(error) throw error
33+
console.log(data.toString())
34+
})
35+
36+
//Eliminar fichero JSON
37+
fs.unlink('info.json', (error: NodeJS.ErrnoException | null) => {
38+
if(error) throw error
39+
console.log('JSON borrado!')
40+
})
41+
42+
43+
44+
// PARA CREAR ARCHIVO XML
45+
46+
// Crear un objeto DOMImplementation
47+
let domImplementation = new ImplementationDOM();
48+
49+
//Crear un objeto XML Document
50+
let xmlBaseDoc: XMLDocument = domImplementation.createDocument(null, 'root');
51+
52+
//Crear elementos y atributos
53+
let main: HTMLElement = xmlBaseDoc.createElement('main');
54+
let nombre: Attr = xmlBaseDoc.createAttribute('nombre');
55+
let edad: Attr = xmlBaseDoc.createAttribute('edad');
56+
let fecha_nacimiento: Attr = xmlBaseDoc.createAttribute('fecha_nacimiento');
57+
let lenguajes: Attr = xmlBaseDoc.createAttribute('lenguajes');
58+
59+
//Agregamos los valores
60+
nombre.value = 'Adrián Iglesias'
61+
edad.value = '19'
62+
fecha_nacimiento.value = '03/11/2004'
63+
lenguajes.value = 'TypeScript, JavaScript, HTML, CSS'
64+
65+
//Agregar elementos al documento
66+
main.setAttributeNode(nombre)
67+
main.setAttributeNode(edad)
68+
main.setAttributeNode(fecha_nacimiento)
69+
main.setAttributeNode(lenguajes)
70+
main.textContent = 'Informacion'
71+
xmlBaseDoc.documentElement.appendChild(main)
72+
73+
//Convertir documento XML en cadena de texto
74+
let xmlString: string = new SerializeXML().serializeToString(xmlBaseDoc)
75+
76+
77+
//Crear fichero XML
78+
fs.writeFile(
79+
'info.xml',
80+
xmlString,
81+
(error: NodeJS.ErrnoException | null) => {
82+
if(error) throw error
83+
console.log('XML creado!')
84+
})
85+
86+
//Leer contenido del fichero XML
87+
fs.readFile('info.xml', (error: NodeJS.ErrnoException | null, data: any) => {
88+
if(error) throw error
89+
let xmlString: string = data.toString()
90+
console.log(xmlString)
91+
})
92+
93+
//Eliminar fichero XML
94+
fs.unlink('info.xml', (error: NodeJS.ErrnoException | null) => {
95+
if(error) throw error
96+
console.log('JSON borrado')
97+
})

0 commit comments

Comments
 (0)