We came up with a helper function like this
/**
* Transforms an ExecuteQuery result into a JS Object resp. JS Object array
* @param {XMLDocument} queryResult a query result
* @param {string} schema the schema name without namespace, e.g. "recipient" instead of "nms:recipient"
* @param {[string]} columns the column names to pull data from and map into the result
* @returns {[object]} an array of JS objects
*/
function queryResultToJS(queryResult, schema, columns) {
/**
* Map a given query result item to a JS object
* @param {object} resultEntry the item mapped to a JS object
*/
function mapItemToObject(resultEntry) {
var result = {};
for (var i = 0; i < columns.length; i++) {
result[columns[i]] = resultEntry.getAttribute(columns[i]);
}
return result;
}
var result = [];
try {
if (queryResult) {
if (queryResult.tagName === schema + '-collection') {
// items in collection or single result
for each(var collectionItem in queryResult.getElementsByTagName(schema)) {
// logInfo('item', JSON.stringify(collectionItem));
result.push(mapItemToObject(collectionItem));
}
} else if (queryResult.tagName === schema) {
// single result
result.push(mapItemToObject(queryResult));
}
}
} catch (error) {
logError('unable to transform result into JS', error);
}
return result;
}
and call it like this
var queryResult = query.ExecuteQuery(),
deliveries = queryResultToJS(queryResult, "delivery", ["id", "label", "clicks"]);
so basically passing in the columns in the result entries.
The result is always an Array of JS objects, event if the result is just a single entry.
Hope that helps,
Björn