Thursday, September 20, 2018

SuiteScript > XML > Use nlapiSelectNode Function


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


Sample code script: 

 

//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 + '</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

var nodeData = nlapiSelectNode(xmlData, path );

 

//print the name of the selected node

nlapiLogExecution('debug','nlapiSelectNode(xmlData, path )', nodeData.nodeName);

 

No comments:

Post a Comment