Change value of a node in an xml

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

Change value of a node in an xml

Post by LasseThid » Wed Oct 09, 2019 1:29 pm

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 2019 Spring Edition, Enfocus PitStop Server 19, Enfocus PDF Review, HP SmartStream 12 & Kodak Prinergy 8.2 with RBA
MAN Roland R705 & R708, KBA Rapida 205, HP Indigo 7600, 7900 & 12000, Inca OnsetX2, Durst P10 among other things

jan_suhr
Advanced member
Posts: 253
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 » Wed Oct 09, 2019 2:45 pm

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
Newbie
Posts: 16
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Change value of a node in an xml

Post by mkayyyy » Wed Oct 09, 2019 2:54 pm

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: 294
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: Change value of a node in an xml

Post by LasseThid » Wed Oct 09, 2019 5:31 pm

Thanks mkayyyy.

It works perfectly!!

Much appreciated!
Enfocus Switch 2019 Spring Edition, Enfocus PitStop Server 19, Enfocus PDF Review, HP SmartStream 12 & Kodak Prinergy 8.2 with RBA
MAN Roland R705 & R708, KBA Rapida 205, HP Indigo 7600, 7900 & 12000, Inca OnsetX2, Durst P10 among other things

Post Reply