N/xml Module Script Samples

The following script samples demonstrate how to use the features of the N/xml module:

These samples reference the following XML file called BookSample.xml:

          <bookstore xmlns:b="http://www.qualifiednamespace.com/book">
    <b:book category="cooking">
        <b:title lang="en">Everyday Italian</b:title>
        <b:author>Giada De Laurentiis</b:author>
        <b:year>2005</b:year>
        <b:price>30.00</b:price>
    </b:book>
    <b:book category="children">
        <b:title lang="en">Harry Potter</b:title>
        <b:author>J K. Rowling</b:author>
        <b:year>2005</b:year>
        <b:price>29.99</b:price>
    </b:book>
    <b:book category="web">
        <b:title lang="en">XQuery Kick Start</b:title>
        <b:author>James McGovern</b:author>
        <b:author>Per Bothner</b:author>
        <b:author>Kurt Cagle</b:author>
        <b:author>James Linn</b:author>
        <b:author>Vaidyanathan Nagarajan</b:author>
        <b:year>2003</b:year>
        <b:price>49.99</b:price>
    </b:book>
    <b:book category="web" cover="paperback">
        <b:title lang="en">Learning XML</b:title>
        <b:author>Erik T. Ray</b:author>
        <b:year>2003</b:year>
        <b:price>39.95</b:price>
    </b:book>
</bookstore> 

        

Load an XML File and Obtain Child Element Values

The following sample loads the BookSample.xml file from the File Cabinet, iterates through the individual book nodes, and accesses the child node values.

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', 'N/file'], function(xml, file) {
    return {
        onRequest: function(options) {
            var sentence = '';
            var xmlFileContent = file.load('SuiteScripts/BookSample.xml').getContents();
            var xmlDocument = xml.Parser.fromString({
                text: xmlFileContent
            });
            var bookNode = xml.XPath.select({
                node: xmlDocument,
                xpath: '//b:book'
            });
            
            for (var i = 0; i < bookNode.length; i++) {
                var title = bookNode[i].firstChild.nextSibling.textContent;
                var author = bookNode[i].getElementsByTagName({
                    tagName: 'b:author'
                })[0].textContent;
                sentence += 'Author: ' + author + ' wrote ' + title + '.\n';
            }

            options.response.write(sentence);
        }
    };
}); 

          

This script produces the following output when used with the BookSample.xml file:

            Author: Giada De Laurentiis wrote Everyday Italian.
Author: J K. Rowling wrote Harry Potter.
Author: James McGovern wrote XQuery Kick Start.
Author: Erik T. Ray wrote Learning XML. 

          

Parse an XML String 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);
            }
        }
    };
}); 

          

Parse an XML File and Append New Elements

The following sample shows how to parse an xml file and append new xml element nodes.

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
 */

require(['N/xml','N/file'], function(xml,file) {
    var xmlData = file.load('SuiteScripts/BookSample.xml').getContents();
    var bookShelf = xml.Parser.fromString({
        text: xmlData
        }); 

    var newBookNode = bookShelf.createElement("book"); 
    var newTitleNode = bookShelf.createElement("title"); 
    var newTitleNodeValue = bookShelf.createTextNode(""); 
    var newAuthorNode = bookShelf.createElement("author"); 
    var newAuthorNodeValue = bookShelf.createTextNode("");

    newBookNode.appendChild(newTitleNode);
    newBookNode.appendChild(newAuthorNode);    
    newTitleNode.appendChild(newTitleNodeValue);
    newAuthorNode.appendChild(newAuthorNodeValue);
    
}); 

          

General Notices