oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
917 stars 108 forks source link

Populating data from an object array #207

Closed tizme closed 5 years ago

tizme commented 5 years ago

I am trying to use an array of objects along with some hard coded valyes to build out an XML file, but can't seem to properly loop through the array

my array looks along the lines of [{title: 'title', time:'time},{{title: 'title', time:'time}]

expected output: <?xml version="1.0" encoding="UTF-8"?>

1x10

I unsuccessfully attempted to achieve with the following (where req.body is my array:

` var root = builder.create('fms_config') .dec('1.0', 'UTF-8') .ele('devince_name'," ") .root() .ele('layout','1x10') .root() .ele('button', {'type': 'Temperature Sensor'}) .ele('display_name',"avocado") .root() for (var i = 0; i < req.body.length; i++) { if (req.body[i].buttonType ==2) { type = "Temperature Sensor" } else { type = "Ingrediant" } var item = root.ele('button', {'type': type}) item.ele('display_name', req.body[i].title) item.ele('initTime', req.body[i].time)

}
root.end({pretty: true})`

Any advice on what I am doing wrong?

tizme commented 5 years ago

Found resolution in closed issues