I would like to find each element's id but I "each" doesnt work (there is no metode each...) How can I return each element's ID?
var libxmljs = require("libxmljs");
var xml = ('/SomePath/filexmlxml');
var xmlDoc2 = libxmljs.parseXmlString(xmlStr);
fs.readFile(__dirname + '/filexml.xml', function (err, data) {
var xmlStr = data.toString();
var id = xmlDoc2.get('Objects').get('Object').attr('id').value(); // It works and get the first element id = 'Title 1'
xmlDoc2.get('Objects').get('Object').each(function () {
var whatid = xmlDoc2.get('Objects').get('Object').attr('id').value();
console.log('wasd: ' + whatid); // Doesnt work - because of each.
})
console.log('idss: ' + id); here I can see the only first's element's ID.
xmlDoc2.get('Objects') will return a Node object. It doesn't have a each method. You can use prevSibling, nextSibling methods to traverse the siblings of the nodes.
Or even better use childNodes on the element.
var children = xmlDoc2.get('Objects').childNodes();
children.forEach(function (child) {
var whatid = child.get('Object').attr('id').value();
console.log('wasd: ' + whatid);
});