How to generate XML string?

amala2
Mega Guru

Hi,

I want to generate a XML for few fields and is having issues when the value of a field is empty? I'm generating the below XML 

For Example: <Number>SNN1234</Number>

But when the Number is empty, Its generated as <Number></Number>. I want it as <Number/>

Please advise.

Thanks

4 REPLIES 4

vkachineni
Kilo Sage
Kilo Sage

How are you generating XML?

 

Here is a code snippet

 

var x = GlideRecord('cmdb_ci_computer');
x.query();
x.next();
var s = gs.unloadRecordToXML(x, true);
gs.print (s);

 

output

 <?xml version="1.0" encoding="UTF-8"?><record_update sys_domain="global" table="cmdb_ci_computer">
    <cmdb_ci_computer action="INSERT_OR_UPDATE">
        <asset display_value="P1000503 - Apple MacBook Pro 15&quot;">04a96c0d3790200044e0bfc8bcbe5db3</asset>
        <asset_tag>P1000503</asset_tag>
        <assigned>2016-11-21 07:00:00</assigned>
        <assigned_to display_value="Eduardo Bellendir">92826bf03710200044e0bfc8bcbe5dbb</assigned_to>
        <assignment_group/>

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

I am working in a scoped app. This is the code I am using:

var xmlDocString = '';
var grDoc = new GlideRecord(tablename);
grDoc.addEncodedQuery('active=true');
grDoc.query();
while (grDoc.next()) {
xmlDocString += '<application_number>' + grDoc.getDisplayValue('application_number') + '</application_number><br/>';
}

gs.info(xmlDocString);

var xmlDocString = '';
var grDoc = new GlideRecord(tablename);
grDoc.addEncodedQuery('active=true');
grDoc.query();
while (grDoc.next()) {
	if(grDoc.getDisplayValue('application_number') == ''){
		xmlDocString += '<application_number/>';
	}
	else
	{
		xmlDocString += '<application_number>' + grDoc.getDisplayValue('application_number') + '</application_number><br/>';
	}
}

gs.info(xmlDocString);
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Any updates?

 

Please mark the question as answered if it did to close it out.

Thank you

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022