How to generate XML string?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 12:17 PM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 12:25 PM
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"">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/>
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 12:37 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 12:43 PM
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);
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2022 09:06 AM
Any updates?
Please mark the question as answered if it did to close it out.
Thank you
Vinod Kumar Kachineni
Community Rising Star 2022