replace xml values with switch values

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

Re: replace xml values with switch values

Post by sander » Wed Jan 20, 2016 11:14 am

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());
}
Part of my playground:
- HP Indigo 12.000 | HP Indigo 10.000 | HP Indigo 7600's | HP Production Center
- Scodix Ultra 2 Pro | Highcon Euclid III | CP Bourg booklet maker (inline w/ 7600)

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

Re: replace xml values with switch values

Post by sander » Wed Jan 20, 2016 2:24 pm

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=""/>
Part of my playground:
- HP Indigo 12.000 | HP Indigo 10.000 | HP Indigo 7600's | HP Production Center
- Scodix Ultra 2 Pro | Highcon Euclid III | CP Bourg booklet maker (inline w/ 7600)

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

Re: replace xml values with switch values

Post by sander » Wed Jan 20, 2016 3:06 pm

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

Good to know it helped you out!
Part of my playground:
- HP Indigo 12.000 | HP Indigo 10.000 | HP Indigo 7600's | HP Production Center
- Scodix Ultra 2 Pro | Highcon Euclid III | CP Bourg booklet maker (inline w/ 7600)

Dave23
Member
Posts: 34
Joined: Thu Oct 12, 2017 4:42 pm

Re: replace xml values with switch values

Post by Dave23 » Tue Jan 29, 2019 1:50 pm

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: 71
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: replace xml values with switch values

Post by Zoranj » Tue Jan 29, 2019 3:02 pm

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: 34
Joined: Thu Oct 12, 2017 4:42 pm

Re: replace xml values with switch values

Post by Dave23 » Tue Jan 29, 2019 3:14 pm

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: 71
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: replace xml values with switch values

Post by Zoranj » Tue Jan 29, 2019 8:20 pm

Yes

Code: Select all

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

Post Reply