How does serialize() method of GlideRecordXMLSerializer() works?

Amit104
Kilo Expert

Hello,

I am trying to create and export an XML file but I am not able to find the correct method to set the value of the element?

Using below block of code, I am able to create an element and its attribute, I am looking for a method that can add a value to the element.

var streamResult = new Packages.javax.xml.transform.stream.StreamResult(outputStream);
var tf = Packages.javax.xml.transform.sax.SAXTransformerFactory.newInstance();
var hd = tf.newTransformerHandler();
var serializer = hd.getTransformer();
serializer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.ENCODING, 'UTF-8');
serializer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, 'yes');
hd.setResult(streamResult);
hd.startDocument();
var attr = new GlidesoftGlideAttributesImpl();
attr.addAttribute("unload_date", GlideSysDateUtil.getUMTDateTimeString());
hd.startElement("", "", 'unload', attr);
var attr.addAttribute("name", "demo");
hd.startElement("", "", 'Parent', attr);
hd.endElement("", "", 'Parent');        
this.hd.endElement("", "", 'unload');
this.hd.endDocument();
outputStream.close();

For this, I would like to know how does the serialize() method of GlideRecordXMLSerializer() works?

Thanks in advance.

4 REPLIES 4

Chuck Tomasi
Tera Patron

The basic purpose of the serialize() function is to. It takes a GlideRecord as an argument and turns it in to XML (just what you would see when you view a record and say Export> XML).

If you're looking for a way to do a record with specific related lists, then you can use the class ExportWithRelatedLists(). I cover this on episode 68 of TechNow.

TechNow Episode List

If you re looking to get the value of the element, then this may be of more help.

Parse XML payloads | ServiceNow Docs

Hello Chuck,

Thanks for your reply. 

The code i have posted is from ExportWithRelatedLists() script include.

I would like to know how the elements are created and how the value are added to the element by the serialize() method. The reason is I am trying to create and export an xml file that will contain only few fields and also customize the value before adding it to an element. (OOTB exports all the fields from the table and we cannot manipulate the data)

I will check episode 68 of TechNow. 

You might want to also take a look at the Payload builder step in IntegrationHub. It allows you to create an XML or JSON payload much easier than with script.

Payload Builder step

  Can we use the ExportWithRelatedLists() class on the other server side scripts(like scripted rest api) rather than the processor?