Sample: create xml from scratch

Post Reply
bens
Advanced member
Posts: 252
Joined: Thu Mar 03, 2011 10:13 am

Sample: create xml from scratch

Post by bens »

Below is a sample showing how to create an xml document using the Switch scripting API. The code is supposed to be used in a script element, with TrafficLight outgoing connections.



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() );

}

dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Sample: create xml from scratch

Post by dkelly »

I've done something very similar. Have you experimented with using or creating namespace maps?
bens
Advanced member
Posts: 252
Joined: Thu Mar 03, 2011 10:13 am

Sample: create xml from scratch

Post by bens »

I have - this complicates things a little, but not that much really.



There are basically 4 changes to the script if you want to use namespaces:



1. ask the xml document to create a new map

2. add the namespace definitions to this map

3. use the namespace prefixes in each element or attribute name that requires them

4. add the map as a second parameter in each of the create... calls.



For example:



var theXmlDoc = new Document();



// 1. create a new map

var theMap = theXmlDoc.createEmptyMap();

// 2. add namespace definitions to the map

theMap.put( "ns1", "http://www.sample.com/ns" );



// unqualified elements don't need the map

var theRootElement = theXmlDoc.createElement( "jobInfo" );

theXmlDoc.setDocumentElement( theRootElement );



// qualified elements:

var theNameElement = theXmlDoc.createElement( "ns1:name" /*3. add the prefix*/, theMap /*4. add the map*/ );

theNameElement.addAttribute( "ns1:prefix" /*3*/, theMap /*4*/, job.getUniqueNamePrefix() );



<snip>

Post Reply