Thursday, September 20, 2018

SuiteScript > XML > Import Nodes Using importNode API


The importNode method has two required parameters namely, node and deep. Node is the node that will be imported. Deep, if set to true allows the subtree underneath the specified node to be imported as well. If deep is set to false, then only the node itself is imported. The node to be imported can be added using the appendChild Node method.

Sample Code Snippet:

 

//We will import the node to this XML

var originalNodeStr = '<?xml version="1.0" encoding="ISO-8859-1"?>';

originalNodeStr = originalNodeStr + '<bookstore>';

originalNodeStr = originalNodeStr + '<book category="cooking">';

originalNodeStr = originalNodeStr + '<title lang="en">Everyday Italian</title>';

originalNodeStr = originalNodeStr + '<author>Giada De Laurentiis</author>';

originalNodeStr = originalNodeStr + '<year>2005</year>';

originalNodeStr = originalNodeStr + '<price>30.00</price>';

originalNodeStr = originalNodeStr + '</book>';

originalNodeStr = originalNodeStr + '</bookstore>';

 

//This XML will be the node to be imported

var importedNodeStr = '<?xml version="1.0" encoding="ISO-8859-1"?>';

importedNodeStr = importedNodeStr + '<bookstore>';

importedNodeStr = importedNodeStr + '<book category="cooking">';

importedNodeStr = importedNodeStr + '<title lang="en">Barefoot Contessa</title>';

importedNodeStr = importedNodeStr + '<author>Ina Garten</author>';

importedNodeStr = importedNodeStr + '<year>2004</year>';

importedNodeStr = importedNodeStr + '<price>60.00</price>';

importedNodeStr = importedNodeStr + '</book>';

importedNodeStr = importedNodeStr + '</bookstore>';

 

//Convert the string data into XML using nlapiStringToXML

var xmlRequestOrig = nlapiStringToXML(originalNodeStr);

var xmlRequestImport = nlapiStringToXML(importedNodeStr);

 

//Declare a variable that will hold the selected node to be imported      

var nodeToBeImported = nlapiSelectNode(xmlRequestImport, "/bookstore/book");

 

//Import the node using importNode method

var importedNode = xmlRequestOrig.importNode(nodeToBeImported,true);

 

//The original XML data now contains the XML for the imported node

var xmlElements = nlapiSelectNode(xmlRequestOrig, "/bookstore");

xmlElements.appendChild(importedNode);

No comments:

Post a Comment