Parse an XML File and Log Element Values
The following sample parses the XML string stored in the xmlString
variable. The sample selects all config
elements in the xmlDocument
node, loops through them, and logs their contents.
Note:
This sample script uses the require
function so that you can copy it into the SuiteScript Debugger and test it. You must use the define
function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
require(['N/xml'], function(xml) {
return {
onRequest: function(options) {
var xmlString = '<?xml version="1.0" encoding="UTF-8"?><config date="1465467658668" transient="false">Some content</config>';
var xmlDocument = xml.Parser.fromString({
text: xmlString
});
var bookNode = xml.XPath.select({
node: xmlDocument,
xpath: '//config'
});
for (var i = 0; i < bookNode.length; i++) {
log.debug('Config content', bookNode[i].textContent);
}
}
};
});