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);
});