|
| 1 | +import * as fs from 'fs'; |
| 2 | + |
| 3 | +interface PersonData { |
| 4 | + name: string; |
| 5 | + age: number; |
| 6 | + birthdate: string; |
| 7 | + programmingLanguage: string[]; |
| 8 | +} |
| 9 | + |
| 10 | +const data: PersonData = { |
| 11 | + name: 'Isaac Cortés', |
| 12 | + age: 22, |
| 13 | + birthdate: "2001-10-21", |
| 14 | + programmingLanguage: ["JavaScript", "Python"] |
| 15 | +}; |
| 16 | + |
| 17 | +fs.writeFileSync('data.json', JSON.stringify(data, null, 4)); |
| 18 | + |
| 19 | +let xmlData2 = `<?xml version ="1.0" encoding="UTF-8"?> |
| 20 | +<person> |
| 21 | + <name>${data.name}</name> |
| 22 | + <age>${data.age}</age> |
| 23 | + <birthdate>${data.birthdate}</birthdate> |
| 24 | + <programmingLanguage>${data.programmingLanguage.map(lang => `<language>${lang}</language>`).join('\n ')}</programmingLanguage> |
| 25 | +</person>`; |
| 26 | + |
| 27 | +fs.writeFileSync('data.xml', xmlData2); |
| 28 | + |
| 29 | +console.log("Content of data.json:"); |
| 30 | +console.log(fs.readFileSync('data.json', 'utf8')); |
| 31 | + |
| 32 | +console.log("\nContent of data.xml:"); |
| 33 | +console.log(fs.readFileSync('data.xml', 'utf8')); |
| 34 | + |
| 35 | +// ** Extra Exercise ** // |
| 36 | + |
| 37 | +class Person { |
| 38 | + name: string; |
| 39 | + age: number; |
| 40 | + birthdate: string; |
| 41 | + programmingLanguage: string[]; |
| 42 | + |
| 43 | + constructor(name: string, age: number, birthdate: string, programmingLanguage: string[]) { |
| 44 | + this.name = name; |
| 45 | + this.age = age; |
| 46 | + this.birthdate = birthdate; |
| 47 | + this.programmingLanguage = programmingLanguage; |
| 48 | + } |
| 49 | + |
| 50 | + showData(): void { |
| 51 | + console.log(`Name: ${this.name}`); |
| 52 | + console.log(`Age: ${this.age}`); |
| 53 | + console.log(`Birthdate: ${this.birthdate}`); |
| 54 | + console.log(`Programming Language: ${this.programmingLanguage.join(', ')}`); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Read JSON and XML file |
| 59 | +const jsonContent: string = fs.readFileSync('data.json', 'utf-8'); |
| 60 | +const xmlContent: string = fs.readFileSync('data.xml', 'utf-8'); |
| 61 | + |
| 62 | +// Parse JSON and XML in JavaScript |
| 63 | +const jsonData: PersonData = JSON.parse(jsonContent); |
| 64 | + |
| 65 | +function parseXML(xml: string): PersonData { |
| 66 | + const name = xml.match(/<name>(.*?)<\/name>/)![1]; |
| 67 | + const age = parseInt(xml.match(/<age>(.*?)<\/age>/)![1], 10); |
| 68 | + const birthdate = xml.match(/<birthdate>(.*?)<\/birthdate>/)![1]; |
| 69 | + const languagesMatches = xml.match(/<language>(.*?)<\/language>/g); |
| 70 | + const programmingLanguage = languagesMatches!.map(lang => lang.replace(/<\/?language>/g, '')); |
| 71 | + |
| 72 | + return { |
| 73 | + name, |
| 74 | + age, |
| 75 | + birthdate, |
| 76 | + programmingLanguage |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +const xmlData: PersonData = parseXML(xmlContent); |
| 81 | + |
| 82 | +// Create instances of the class with the read data |
| 83 | +const personFromJson = new Person( |
| 84 | + jsonData.name, |
| 85 | + jsonData.age, |
| 86 | + jsonData.birthdate, |
| 87 | + jsonData.programmingLanguage |
| 88 | +); |
| 89 | + |
| 90 | +const personFromXml = new Person( |
| 91 | + xmlData.name, |
| 92 | + xmlData.age, |
| 93 | + xmlData.birthdate, |
| 94 | + xmlData.programmingLanguage |
| 95 | +); |
| 96 | + |
| 97 | +// Show data for class instances |
| 98 | +console.log("Data from JSON:"); |
| 99 | +personFromJson.showData(); |
| 100 | + |
| 101 | +console.log("\nData from XML:"); |
| 102 | +personFromXml.showData(); |
| 103 | + |
| 104 | +// Delete the files |
| 105 | +fs.unlinkSync('data.json'); |
| 106 | +fs.unlinkSync('data.xml'); |
| 107 | +console.log("\nDeleted files."); |
0 commit comments