How to Read Values from the XML Payload

vishal065
Tera Expert

Hi,

I am facing to fetch the node value of one the attributes. In my XML Payload, the response is something like:

<domain>europe.abc.com</domain>

Or

<domain>pacific.abc.com</domain>

Or

<domain>americas.abc.com</domain>

Or

<domain>abc.com</domain>

I need to read the first value (before dot) of the attribute <domain>.

So my desired output is: europe Or pacific Or americas Or abc.

I am using below line to get the value but i am getting 'undefined' always

var dom = xmldoc.getNodetext("//domain")

Please advise if anyone knows the solution of it.

Regards
Vishal

1 ACCEPTED SOLUTION

Hi Vishal,



The dom variable should print the value "europe.abc.com" and you wanted to retrieve the value "europe" so as I mentioned earlier in my comment you will have to split using dot



var dom = "europe.abc.com"; // this you are getting as per your comment


var splitValues = dom.split(".");


var firstValue = splitValues[0]; // this prints europe


var secondValue = splitValues[1]; // this prints abc


var thirdValue = splitValues[2]; // this prints com



Can you try and let me know?



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vishal,



Following is the way to fetch the value within the <domain> tag. once you get that value you can split using dot and get the first value as below



var str = ''; // your xml document


var xmldoc = new XMLDocument(str);


var value = xmldoc.getNodeText("//domain");


gs.print(value);


var splitValues = value.split("."); // split with dot


gs.print(splitValues[0]); // this should give you europe



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



Thanks for your response.



I tried to use this method but it gives 'undefined' result to me. Please see the script


var dom = parseString(xmldoc.getNodetext("//domain"));


  var splitValues = dom.split(".");


  //var domName = gs.print(splitDom[0]);


  gs.log("Payload Values: " + correlationID + " " + name + " " + " " + os + splitValues);



The logs gives correct value if I comment the 2nd line and just print the first variable i.e., 'dom' (Result: europe.abc.com)



But when I use split method it doesn't show any value at all.



I also tried to use .toString() and parseString() methods but neither works.



Will be great if you can suggest any solution to overcome this issue.



Regards


Vishal


deepak.ingale


Hi Vishal,



The dom variable should print the value "europe.abc.com" and you wanted to retrieve the value "europe" so as I mentioned earlier in my comment you will have to split using dot



var dom = "europe.abc.com"; // this you are getting as per your comment


var splitValues = dom.split(".");


var firstValue = splitValues[0]; // this prints europe


var secondValue = splitValues[1]; // this prints abc


var thirdValue = splitValues[2]; // this prints com



Can you try and let me know?



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



This is working. It was creating an issue because of small syntax error.



Thanks for the assistance.



Regards,


Vishal