-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseXML.js
More file actions
70 lines (58 loc) · 2.58 KB
/
Copy pathparseXML.js
File metadata and controls
70 lines (58 loc) · 2.58 KB
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
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs');
const flow = require('xml-flow');
const { addValue } = require('./leveldb/level.js');
function parseXML() {
return new Promise((resolve, reject) => {
const rawXMLFile = fs.createReadStream('./menu.xml');
const xmlStream = flow(rawXMLFile);
xmlStream.on('tag:food', async (food) => {
try {
console.log('Processing food item:', JSON.stringify(food, null, 2));
if (!food || !food.title) {
console.error('Invalid food item structure:', food);
return;
}
const foodName = food.title;
const foodInfo = {
id: food.$attrs && food.$attrs.id || null,
title: foodName,
meats: food.meats === 'true',
grains: food.grains === 'true',
dairy: food.dairy === 'true',
fruitveg: food.fruitveg === 'true',
fatsoils: food.fatsoils === 'true',
location: food.location || null,
time: food.time || null,
calories: parseInt(food.calories) || null,
carbs: parseInt(food.carbs) || null,
protein: parseInt(food.protein) || null,
fat: parseInt(food.fat) || null,
sugars: parseInt(food.sugars) || null,
sodium: parseInt(food.sodium) || null,
dietaryfiber: parseInt(food.dietaryfiber) || null,
vegan: food.vegan === 'true',
gluten: food.gluten === 'true',
servingsize: parseFloat(food.servingsize) || null,
description: food.description || null
};
if (!foodName) {
console.error('Food name is empty or undefined');
return;
}
await addValue(foodName, JSON.stringify(foodInfo));
console.log(`Added ${foodName} to the database`);
} catch (error) {
console.error(`Error processing food item: ${error.message}`, food);
}
});
xmlStream.on('end', () => {
console.log('Finished parsing XML and adding to database');
resolve();
});
xmlStream.on('error', (error) => {
console.error('Error parsing XML:', error);
reject(error);
});
});
}
module.exports = { parseXML };