Skip to Content
Author's profile photo Former Member

Parse XML in server side javascript (XSJS)

Hi All,

As Thomas Jung explains how to use SAP HANA SP06 features here in blog SAP HANA SPS6 – Various New Developer Features

SP6 offers a new functionality of Outbound Data Connectivity. In his blog Thomas explains about parsing JSON format in the server side javascripting (XSJS). But what if the data is available only in XML.

This was the problem that I was facing to connect to one of the web services. After searching in vain at many places but not getting a proper guide to parse XML in XSJS file I decided to make a blog of how I have achieved this. I agree that there could be better ways to parse XML.

Follow the above mentioned blog to get the response from the web service

  • Create a “.xshttpdest” file as explained in one of the Videos here from 8:50 to around 11 minutes. Modify this file where you feel is required.
  • Create a “.xsjs”file from the sample provided and modify this as per your requirement.

A snippet of the useful code.

var action = $.request.parameters.get("action"); 
var dest = $.net.http.readDestination("", "user"); 
var client = new $.net.http.Client(); 
var req = new $.web.WebRequest($.net.http.GET, action); 
client.request(req, dest); 
var response = client.getResponse(); 

  • Convert the response to string using
xmlString = response.body.asString();

  • Now one has to actually parse this string to get values out of the XML. Use the function below to parse this XML
function getValue(tag,xmlString){
    var value;
    var tempString;
    var startTag,endTag;
    var startPos,endPos;
    startTag = "<"+tag+">";
    endTag = "</"+tag+">";
    startPos = + startTag.length;
    endPos =;
    value = tempString.slice(startPos,endPos);
    return value;

This function takes in parameters xmlString which is plain string in XML format and tag for which one needs the value. It returns the value inside the tags.

So if the XML looks like

     <body>Don't forget me this weekend!</body>

The function

var from = getValue("from",xmlString) 

returns “Jani” to the variable from

This approach lacks many things for sure, but they can be added as per requirement.

I am welcome all your elegant solutions to parse XML in server side javascript.Kindly reply to this thread Outbound data connectivity-parsing XML in server side javascript if you have solutions for this problem


Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Shreepad,

      Nice blog. Very informative.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thanks Keerthan

      Author's profile photo Former Member
      Former Member

      Great post.. 🙂