replace xml values with switch values

Post Reply
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: replace xml values with switch values

Post by sander »

I would read the entire XML, replace whatever you want, write back to a new file.

Replace whatever value you want like I do with the lineBreaks over here;

Code: Select all

function jobArrived( s : Switch, job : Job )
{

		// Get propertyValue
		var privateDataKey = s.getPropertyValue('PrivateData');
		
		// Create file with Windows-1252 coding, needed for certain characters
		var readBody = File.read(job.getPath(), "Windows-1252");
		
		// Replace LF with CRLF, Prodist needs CRLF for correct parsing of e.g. addresses
		var replaceLineBreak = readBody.replace(/\n/g,"\r\n");
		
		// Put body in private data
		job.setPrivateData(privateDataKey, replaceLineBreak);
		
		job.sendToSingle(job.getPath());
}
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: replace xml values with switch values

Post by sander »

Haha fair enough. I just copied this script as reference/idea for the replace part.

Just tested and came up with this one.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	// Read properties	
	var oldValue1 = s.getPropertyValue('oldValue1');
	var newValue1 = s.getPropertyValue('newValue1');	
	
	// Read incoming XML
 	var oldXML = File.read(job.getPath(), "Windows-1252");
	
	// Create new XML
	var newXML = job.createPathWithName(job.getName());
	
	// Replace stuff
	// These properties can easily be filled via standard Switch properties, see screenshot.
	var newXMLcontent = oldXML.replace(oldValue1,newValue1);
	
	// Write new XML
	File.write(newXML, newXMLcontent, 'Windows-1252');
	
	// Send newXML to output
	job.sendToSingle(newXML);	
}
Image

oldXML input:

Code: Select all

<print duplex="yes" sides_printed="4/4" sides_printed_text="dubbelzijdig full colour" color_space="CMYK" collate="" facing_print="" page_order="" divider_sheet=""/>
newXML output:

Code: Select all

<print duplex="yes" sides_printed="Hooray" sides_printed_text="dubbelzijdig full colour" color_space="CMYK" collate="" facing_print="" page_order="" divider_sheet=""/>
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: replace xml values with switch values

Post by sander »

If you have the scripting module, the sky is the limit :D

Good to know it helped you out!
Dave23
Member
Posts: 41
Joined: Thu Oct 12, 2017 4:42 pm

Re: replace xml values with switch values

Post by Dave23 »

Hi all,

How would this particular script work with multiple values?

Thanks

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	// Read properties	
	var oldValue1 = s.getPropertyValue('oldValue1');
	var newValue1 = s.getPropertyValue('newValue1');
	var oldValue2 = s.getPropertyValue('oldValue2');
	var newValue2 = s.getPropertyValue('newValue2');	
	
	// Read incoming XML
 	var oldXML = File.read(job.getPath(), "Windows-1252");
	
	// Create new XML
	var newXML = job.createPathWithName(job.getName());
	
	// Replace stuff
	// These properties can easily be filled via standard Switch properties, see screenshot.
	var newXMLcontent = oldXML.replace(oldValue1,newValue1);
	
	// Write new XML
	File.write(newXML, newXMLcontent, 'Windows-1252');
	
	// Send newXML to output
	job.sendToSingle(newXML);	
}
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: replace xml values with switch values

Post by Zoranj »

It works fine but limit is 3 values to replace for some reason.
If you add 4th, it stops working.

You could daisy chain and add script as many times as you want, times 3 values.
Dave23
Member
Posts: 41
Joined: Thu Oct 12, 2017 4:42 pm

Re: replace xml values with switch values

Post by Dave23 »

Hi zoranj

I've tried it with two using the script below, but that doesn't seem to work. It only takes into account the first value change.

Do i not need to amend this part of the code as well?

Code: Select all

var newXMLcontent = oldXML.replace(oldValue1,newValue1);
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: replace xml values with switch values

Post by Zoranj »

Yes

Code: Select all

var newXMLcontent = oldXML.replace(oldValue1,newValue1).replace(oldValue2,newValue2);
Post Reply