-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.ts
59 lines (50 loc) · 1.54 KB
/
convert.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createReadStream } from 'fs';
import { Parser } from 'node-expat';
import { createObjectCsvWriter as createCsvWriter } from 'csv-writer';
// Function to stream XML and convert it to CSV
function streamXmlToCsv(xmlFilePath: string, csvFilePath: string) {
const stream = createReadStream(xmlFilePath);
const parser = new Parser();
const csvWriter = createCsvWriter({
path: csvFilePath,
header: [
{ id: 'date', title: 'DATE' },
{ id: 'weight', title: 'WEIGHT' },
],
});
let isRecord = false;
let recordAttrs: any = {};
let records: Array<{ date: string; weight: string }> = [];
parser.on('startElement', (name, attrs) => {
if (name === 'Record' && attrs.type === 'HKQuantityTypeIdentifierBodyMass') {
isRecord = true;
recordAttrs = attrs;
}
});
parser.on('endElement', (name) => {
if (isRecord && name === 'Record') {
const record = {
date: new Date(recordAttrs.startDate).toLocaleDateString('en-GB'), // Converts to DD/MM/YYYY format
weight: recordAttrs.value,
};
records.push(record);
isRecord = false;
recordAttrs = {};
}
});
parser.on('end', async () => {
await csvWriter.writeRecords(records)
.then(() => console.log('CSV file has been written'));
});
stream.pipe(parser).on('error', (error) => {
console.error('Error processing XML with node-expat:', error);
});
}
async function main() {
try {
await streamXmlToCsv('export.xml', 'weight.csv');
} catch (error) {
console.error(error);
}
}
main();