Friday, September 21, 2018

SuiteScript > XML API > nlapiSelectNodes Function Sample Code

nlapiSelectNodes is an XML API. It accepts two parameters namely, node and xpath. Both of which are required. In order to use nlapiSelectNodes, users will need an XML data. The following code shows how to obtain an XML data from a string and how to make use of nlapiSelectNodes in order to select the multiple nodes from the XML data.

Note: nlapiSelectNodes returns an array of nodes.

Sample code snippet in Javascript: 

//declare a variable that will hold the XML in the form of a string

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

xmlStringVar = xmlStringVar + '<bookstore>';

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

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

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

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

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

xmlStringVar = xmlStringVar + '<title lang="en">French Today</title>';

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

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

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

xmlStringVar = xmlStringVar + '</book>';

xmlStringVar = xmlStringVar + '</bookstore>';

 

//convert the string data into XML using nlapiStringToXML

var xmlData = nlapiStringToXML(xmlStringVar);

 

//declare a variable that will hold the xpath

path = "/bookstore/book/title";

 

//declare a variable that will hold the selected node

nodeData = nlapiSelectNodes(xmlData, path );
 
//print the values of the selected nodes
for(var i = 0; i < nodeData.length; i++){
   nlapiLogExecution('DEBUG','nlapiSelectNodes(xmlData, path)',nodeData[i].firstChild.nodeValue);

}

 

----

 

Result:

Two results = 'Everyday Italian' and 'French Today'.

 



 

No comments:

Post a Comment