Skip to content

Commit b3ca5ad

Browse files
committed
#12 - TypeScript & JavaScript
1 parent d477106 commit b3ca5ad

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var fs = require('fs');
2+
var readlineSync = require('readline-sync');
3+
var _a = require('xmldom'), ImplementationDOM = _a.DOMImplementation, SerializeXML = _a.XMLSerializer;
4+
var jsonContent = {
5+
nombre: 'Andres Chapeton',
6+
edad: 26,
7+
fecha_de_nacimiento: '02/06/1997',
8+
lista_lenguajes: ['TypeScript', 'JavaScript', 'Python', 'C#']
9+
};
10+
fs.writeFile('data.json', JSON.stringify(jsonContent), function (error) {
11+
if (error)
12+
throw error;
13+
console.log('JSON file created');
14+
});
15+
//Leer contenido del fichero JSON
16+
fs.readFile('data.json', function (error, data) {
17+
if (error)
18+
throw error;
19+
console.log(data.toString());
20+
});
21+
//Eliminar fichero JSON
22+
fs.unlink('data.json', function (error) {
23+
if (error)
24+
throw error;
25+
console.log('JSON file deleted');
26+
});
27+
// PARA CREAR ARCHIVO XML
28+
// Crear un objeto DOMImplementation
29+
var domImplementation = new ImplementationDOM();
30+
//Crear un objeto XML Document
31+
var xmlBaseDoc = domImplementation.createDocument(null, 'root');
32+
//Crear elementos y atributos
33+
var main = xmlBaseDoc.createElement('main');
34+
var full_name = xmlBaseDoc.createAttribute('full_name');
35+
var age = xmlBaseDoc.createAttribute('age');
36+
var birth = xmlBaseDoc.createAttribute('birth');
37+
var list = xmlBaseDoc.createAttribute('list');
38+
//Agregar valures
39+
full_name.value = 'Andres Chapeton';
40+
age.value = '26';
41+
birth.value = '02/06/1997';
42+
list.value = 'TypeScript, JavaScript, Python, C#';
43+
//Agregar elementos al documento
44+
main.setAttributeNode(full_name);
45+
main.setAttributeNode(age);
46+
main.setAttributeNode(birth);
47+
main.setAttributeNode(list);
48+
main.textContent = 'Datos';
49+
xmlBaseDoc.documentElement.appendChild(main);
50+
//Convertir documento XML en cadena de texto
51+
var xmlString = new SerializeXML().serializeToString(xmlBaseDoc);
52+
//Crear fichero XML
53+
fs.writeFile('data.xml', xmlString, function (error) {
54+
if (error)
55+
throw error;
56+
console.log('XML file created');
57+
});
58+
//Leer contenido del fichero XML
59+
fs.readFile('data.xml', function (error, data) {
60+
if (error)
61+
throw error;
62+
var xmlString = data.toString();
63+
console.log(xmlString);
64+
});
65+
//Eliminar fichero XML
66+
fs.unlink('data.xml', function (error) {
67+
if (error)
68+
throw error;
69+
console.log('JSON file deleted');
70+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const fs = require('fs')
2+
const readlineSync = require('readline-sync');
3+
const { DOMImplementation: ImplementationDOM, XMLSerializer: SerializeXML } = require('xmldom');
4+
5+
// PARA CREAR ARCHIVO JSON
6+
7+
interface Content {
8+
nombre: string,
9+
edad: number,
10+
fecha_de_nacimiento: string,
11+
lista_lenguajes: Array<string>
12+
}
13+
14+
let jsonContent: Content = {
15+
nombre: 'Andres Chapeton',
16+
edad: 26,
17+
fecha_de_nacimiento: '02/06/1997',
18+
lista_lenguajes: ['TypeScript', 'JavaScript', 'Python', 'C#']
19+
}
20+
21+
fs.writeFile(
22+
'data.json',
23+
JSON.stringify(jsonContent),
24+
(error: NodeJS.ErrnoException | null) => {
25+
if(error) throw error
26+
console.log('JSON file created')
27+
})
28+
29+
//Leer contenido del fichero JSON
30+
fs.readFile('data.json', (error: NodeJS.ErrnoException | null, data: any) => {
31+
if(error) throw error
32+
console.log(data.toString())
33+
})
34+
35+
//Eliminar fichero JSON
36+
fs.unlink('data.json', (error: NodeJS.ErrnoException | null) => {
37+
if(error) throw error
38+
console.log('JSON file deleted')
39+
})
40+
41+
42+
43+
// PARA CREAR ARCHIVO XML
44+
45+
// Crear un objeto DOMImplementation
46+
const domImplementation = new ImplementationDOM();
47+
48+
//Crear un objeto XML Document
49+
const xmlBaseDoc: XMLDocument = domImplementation.createDocument(null, 'root')
50+
51+
//Crear elementos y atributos
52+
const main: HTMLElement = xmlBaseDoc.createElement('main')
53+
const full_name: Attr = xmlBaseDoc.createAttribute('full_name')
54+
const age: Attr = xmlBaseDoc.createAttribute('age')
55+
const birth: Attr = xmlBaseDoc.createAttribute('birth')
56+
const list: Attr = xmlBaseDoc.createAttribute('list')
57+
58+
//Agregar valures
59+
full_name.value = 'Andres Chapeton'
60+
age.value = '26'
61+
birth.value = '02/06/1997'
62+
list.value = 'TypeScript, JavaScript, Python, C#'
63+
64+
//Agregar elementos al documento
65+
main.setAttributeNode(full_name)
66+
main.setAttributeNode(age)
67+
main.setAttributeNode(birth)
68+
main.setAttributeNode(list)
69+
main.textContent = 'Datos'
70+
xmlBaseDoc.documentElement.appendChild(main)
71+
72+
//Convertir documento XML en cadena de texto
73+
const xmlString: string = new SerializeXML().serializeToString(xmlBaseDoc)
74+
75+
76+
//Crear fichero XML
77+
fs.writeFile(
78+
'data.xml',
79+
xmlString,
80+
(error: NodeJS.ErrnoException | null) => {
81+
if(error) throw error
82+
console.log('XML file created')
83+
})
84+
85+
//Leer contenido del fichero XML
86+
fs.readFile('data.xml', (error: NodeJS.ErrnoException | null, data: any) => {
87+
if(error) throw error
88+
const xmlString: string = data.toString()
89+
console.log(xmlString)
90+
})
91+
92+
//Eliminar fichero XML
93+
fs.unlink('data.xml', (error: NodeJS.ErrnoException | null) => {
94+
if(error) throw error
95+
console.log('JSON file deleted')
96+
})

0 commit comments

Comments
 (0)