Skip to Content

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.
host="www.XYZ.com";
port=80;
description="XYZ";
pathPrefix="/getXMLdata?action=";
authType=none;
useProxy=false;
proxyHost="";
proxyPort=0;
timeout=0;
  • 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("sp6.services", "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+">";
    tempString=xmlString;
    startPos = tempString.search(startTag) + startTag.length;
    endPos = tempString.search(endTag);
    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

<note>
     <to>Tove</to>
     <from>Jani</from>
     <heading>Reminder</heading>
     <body>Don't forget me this weekend!</body>
</note>

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

–Shreepad

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply