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