I have the following nested XML, which I would like to stream parse with Node.js to a Postgres database. The XML is reduced to a reproducible example, but is in fact large.
<MarketDocument> <createdDateTime>2018-02-17T16:42:28Z</createdDateTime> <TimeSeries> <Type>A01<Type> <Period> <Point><position>1</position></Point> <Point><position>2</position></Point> </Period> </TimeSeries> <TimeSeries> <Type>B01<Type> <Period> <Point><position>3</position></Point> <Point><position>4</position></Point> </Period> </TimeSeries> </MarketDocument> Expected output: [["A01", 1], ["A01", 2], ["B01", 3], ["B01", 4]]
Main problem: iterating over the parent (<Type>). Haven't found good documentation on this problem. Would like to work along the approach by forrert
Question:
1) Do you have an idea to parse this correctly with Node.js?
2) Maybe there is another approach: let me know.
I basically need help with the following part:
var XmlStream = require('xml-stream'); var stream = fs.createReadStream('./here.xml'); // or stream directly from your online source var xml = new XmlStream(stream); xml.on('endElement: TimeSeries', function(item) { // PHP-code: How do you do this in nodejs foreach ($item->Period->Point as $point) { $position = $point->position; $array[] = "('$Type', '$position')"; } }); Your help would be appreciated!
arr.push([type, pos])?