Page 1 of 1
Re: replace xml values with switch values
Posted: Wed Jan 20, 2016 11:14 am
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());
}
Re: replace xml values with switch values
Posted: Wed Jan 20, 2016 2:24 pm
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);
}
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=""/>
Re: replace xml values with switch values
Posted: Wed Jan 20, 2016 3:06 pm
by sander
If you have the scripting module, the sky is the limit
Good to know it helped you out!
Re: replace xml values with switch values
Posted: Tue Jan 29, 2019 1:50 pm
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);
}
Re: replace xml values with switch values
Posted: Tue Jan 29, 2019 3:02 pm
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.
Re: replace xml values with switch values
Posted: Tue Jan 29, 2019 3:14 pm
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);
Re: replace xml values with switch values
Posted: Tue Jan 29, 2019 8:20 pm
by Zoranj
Yes
Code: Select all
var newXMLcontent = oldXML.replace(oldValue1,newValue1).replace(oldValue2,newValue2);