-
Notifications
You must be signed in to change notification settings - Fork 4
/
xml_to_json.js
executable file
·42 lines (38 loc) · 1.08 KB
/
xml_to_json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/env bun
import * as fs from 'fs';
import { XMLParser } from 'fast-xml-parser';
// read data from stdin
const data = fs.readFileSync(0, 'utf-8');
const options = {
// We want to make sure collections of length 1 are still parsed as arrays
isArray: (name, jpath, isLeafNode, isAttribute) => {
const collectionNames = [
'mur',
'plancher_bas',
'plancher_haut',
'baie_vitree',
'porte',
'pont_thermique',
'ventilation',
'installation_ecs',
'generateur_ecs',
'climatisation',
'installation_chauffage',
'generateur_chauffage',
'emetteur_chauffage',
'sortie_par_energie'
];
if (collectionNames.includes(name)) return true;
},
tagValueProcessor: (tagName, val) => {
if (tagName.startsWith('enum_')) {
// Preserve value as string for tags starting with "enum_"
return null;
}
if (Number.isNaN(Number(val))) return val;
return Number(val);
}
};
const parser = new XMLParser(options);
const dpe_json = parser.parse(data).dpe;
console.log(JSON.stringify(dpe_json, null, 2));