There are plenty of comments, so I hope it's easy enough to read

//***************************
// This sample shows how to create an xml document from scratch using the Switch scripting API.
// We create a very short xml document containing some information about the incoming job. This
// shows how to create an xml documents, and add a root element, composed elements, text elements,
// attributes and comments.
//
// Adding new elements to an xml document always consists of 2 steps:
// 1. ask the document to create a new element
// 2. add the element to its parent
// The only exception are attributes, which can be added without the separate creation step.
//***************************
const cSuccess = 1; // for readability
// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
// 1. create a new (empty) xml document
var theXmlDoc = new Document();
// 2. a) create a root element
var theRootElement = theXmlDoc.createElement( "jobInfo" );
// b) and add it to the document
theXmlDoc.setDocumentElement( theRootElement );
// 3. create the xml structure
// a) create the elements
// - normal elements
var theNameElement = theXmlDoc.createElement( "name" );
// - attributes
theNameElement.addAttribute( "prefix" /*name*/, null /*namespace map*/, job.getUniqueNamePrefix() /*value*/ );
// - text elements
var theTextElement = theXmlDoc.createText( job.getName() );
// - comments
var theComment = theXmlDoc.createComment( "other information omitted" );
// b) and add each to its parent
theRootElement.appendChild( theNameElement );
theNameElement.appendChild( theTextElement );
theRootElement.appendChild( theComment );
// 4. save the xml document to a temp path
var theTempPath = job.createPathWithName( job.getNameProper() + ".xml" );
theXmlDoc.save( theTempPath );
// 5. send the result further in the flow
job.sendToLog( cSuccess, theTempPath );
job.sendToData( cSuccess, job.getPath() );
}