Reputation: 683
I need a powerful package to parse large XML files, and I came across xml-stream. It is great, but for some reason I can't seem to find how to parse unknown nodes, it expects me to send the expected field as a parameter.
That's the XML file
<Offers>
<Offer>
<Model>
694547
</Model>
<RegularPrice>
219.00
</RegularPrice>
<ParentSKU>
654987
</ParentSKU>
</Offer>
<Offer>
<Model>
694548
</Model>
<RegularPrice>
235.00
</RegularPrice>
<ParentSKU>
123456
</ParentSKU>
</Offer>
And this is the code I use
var XmlStream = require('xml-stream');
var fs = require('fs');
var stream = fs.createReadStream(filePath);
var xml = new XmlStream(stream);
xml.on('endElement: Offer', function (item) { //<<<- Here I need to add 'Offer' as an expected node
console.log('loaded', JSON.stringify(item));
});
xml.on('end', function () {
console.log('END XML');
});
The response is great, as expected
loaded {"Model":"694547","RegularPrice":"219.00","ParentSKU":"654987"}
loaded {"Model":"694548","RegularPrice":"235.00","ParentSKU":"123456"}
END XML
How can I get the same list without me having to set 'Offer' as an expected node? The XML's I'm expecting to get can have different nodes and values.
Upvotes: 1
Views: 2695
Reputation: 683
ok, in the end - I've override the xml-stream lib. (located in '/xml-stream/lib/xml-stream.js')
xml.on('endElement', function(name) {
//xml-stream code
if (self._bufferLevel === 0 && self._emitData) {
emitEnd.call(self, name);
}
//my override <<-----
if (prev.path.split('/').length == 2) { //the split length check give me the first child
self.emit('endItem', val);
}
//my override end
curr = prev;
this._collect = curr.collect;
});
to implement
xml.on('endItem', function (item) {
//single item
});
Upvotes: 2