0

Is there a way to parse XML in SSJS. I have an RSS Feed that comes as XML in the response, is there a way I can parse the field and store them in a Data Extension, using SSJS.

I have tried standard Java Script functions but they do not work in SSJS.

Any help is appreciated. Thanks.

5

There are no SSJS functions to parse XML, but we have achieved this with SSJS by using regular expressions to parse the XML, which works really well. Here's a snippet to set you in the right direction:

function trim(x) {
  return x.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};

function dataFromAttr(data, attr, notNull){
  if (data){
    var reg = new RegExp("<"+attr+">", "g");
    var result = data.match(reg);
    if(result.length == 0){
      var str = '';
      return trim(str);
    }
    if(result.length == 1){
      var str = data.split("<"+attr+">")[1];
      str = str.split("</"+attr+">")[0];
      return trim(str);
    }
    if(result.length > 1){
      var str = data.split("</"+attr+"> <"+attr+">");
      return str;
    }
    var nullElementValue = new RegExp("<"+attr+" />");
    if(data.match(nullElementValue).length) return "";
    if(notNull) return "";
  }
  else{
    return null;
  }
};

    var phHome = dataFromAttr(client, "home_phone");
    var phMobile = dataFromAttr(client, "mobile_phone");

In this instance, this script retrieving the home_phone and mobile_phone values from a client element in my XML:

<client>
    <home_phone>0712345678</home_phone>
    <mobile_phone>0402123456</mobile_phone>
    <fax_number />
</client>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.