Change value of a node in an xml

Post Reply
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Change value of a node in an xml

Post by LasseThid »

Sometimes I need to change the value of the key sku inside of the xml files we receive from our customer. The sku sometimes needs to be changed to CCSE-9999 due to production changes.
In order to make it easier for my colleagues to do this I'm thinking about setting up a small flow where they can drop the incoming xml and then the modified xml will replace the old xml.

The xml looks like this:

Code: Select all

<?xml version="1.0"?>
<order>
	<orderId>9986613</orderId>
	<incrementId>XXXXXX</incrementId>
	<versionNumber>1</versionNumber>
	<name>XXXXX</name>
	<createdDate>2019-10-08T17:53:59+0200</createdDate>
	<orderDate>2019-10-08T15:51:38+0200</orderDate>
	<description>Adshel, Eurosize (1185x1750mm), OFFSET 4+3</description>
	<sku>CCNO-04</sku>
	<externalOrderId>108573</externalOrderId>
</order>
How can I do this?
Thanks in advance
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Change value of a node in an xml

Post by jan_suhr »

Well, you need an XSLT to do that.

Or change it with a script

Should it always be changed to CCSE-9999

Jan
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
mkayyyy
Member
Posts: 75
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Change value of a node in an xml

Post by mkayyyy »

The below script will replace the SKU node with a new SKU node with the value set to "CCSE-9999"

Code: Select all

// 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 ) {
	// Create XML document object
	var xmlDoc = new Document(job.getPath());
	
	// Xpath to //order/sku node
	var skuNode = xmlDoc.evalToNode("//order/sku");
	
	// Create new SKU node and create text value of CCSE-9999
	var newSKUNode = xmlDoc.createElement("sku");
	var newSKUVal = xmlDoc.createText("CCSE-9999");
	
	// Append text value onto new SKU node
	newSKUNode.appendChild(newSKUVal);
	// Replace old SKU node with new SKU node and save the edited XML
	xmlDoc.replaceChild(newSKUNode, skuNode);
	xmlDoc.save(job.getPath());
	
	// Send to outgoing connection
	job.sendToSingle(job.getPath())
}
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: Change value of a node in an xml

Post by LasseThid »

Thanks mkayyyy.

It works perfectly!!

Much appreciated!
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
Post Reply