Page 1 of 1
Replacing values in an XML by a script
Posted: Wed Jul 13, 2011 11:15 pm
by reprokaiser
Dear All,
after Bens quite nice primer on creating an XML file it's mostly clear how to author such a file. However, I ran into a problem using those methods: how can I actually change a value in an existing XML by using a script?
After studying the manual and the reference I can't help just to acquire all the data out of an existing XML and create a new one with the altered data. There has to be an easier way to change just one value in a node, without re-creating the whole file. Example:
original:
ABCDE
after the script I want to have a file like this:
FGHIJK
I guess this has to be pretty simple, just I can't find any reference to this. Any help appreciated,
Kind regards,
Peter Nagy
Replacing values in an XML by a script
Posted: Thu Jul 14, 2011 10:26 am
by bens
Szia Peter,
Unfortunately the only way to change an XML is to completely re-write it. But this is not really as bad as it sounds: the whole parsing and rewriting part is done in the background. There are only 4 steps to go through:
1. open the XML
This is a single line:
var xmlDocument = new Document( "/path/to/file.xml" );
2. locate (the parent of) the node you want to change
Depending on your needs, this too may be a single line:
var theNode = xmlDocument.evalToNode( "/jobInfo/code" );
3. change the node
This involves getting the node you want to change (=the existing node), creating a new node, and calling replaceChild(). For example:
var theNewTextNode = xmlDoc.createText( "FGHIJK" );
theNode.replaceChild( theNewTextNode, theNode.getFirstChild() );
4. save the file
Again, a single line:
xmlDoc.save( "/path/to/newfile.xml" );
In a real situation you'll have more code than this of course - but for your example these 5 lines already work

Replacing values in an XML by a script
Posted: Thu Jul 14, 2011 3:33 pm
by reprokaiser
Hi Bens,
thanks for the quick reply, it's working perfectly!
Kind regards,
Peter
Replacing values in an XML by a script
Posted: Wed Jul 20, 2011 2:47 pm
by Clive Andrews
OK - I'm still in trouble...
To try and get my head round this - it took your script parts, and edited them to initially just change the job number:
var xmlDocument = new Document ( "C:/Tests/101615.jdf" );
var theNode = xmlDocument.evalToNode( "/JDF/JobID" );
var theNewTextNode = xmlDoc.createText( "103999" );
theNode.replaceChild theNewTextNode, theNode.getFirstChild();
xmlDoc.save( "c:/Tests/103999.jdf" );
But all I'm getting is "Expected end of statement" error - 800A0401
Mr Microsoft says I'm missing something - but after half a day of trawling and editing - I can't see what. My theory is if I can prove this, I want to use the same way to extract data from a node - and pass it onto another (or as part of the same) script...
I know, new boy, doesn't understand and all that - but I'm getting there virtually self taught!
Replacing values in an XML by a script
Posted: Wed Jul 20, 2011 4:26 pm
by dkelly
Clive, the syntax error was caused by missing parentheses.
theNode.replaceChild( theNewTextNode, theNode.getFirstChild() );
However, the code won't work even after you fix this problem because JobID is an attribute not an element. Here's the correct method.
var xmlDocument = new Document( "C:/Tests/101615.jdf" );
if (xmlDocument != null) {
var theElem = xmlDocument.getDocumentElement();
theElem.addAttribute( "JobID", null, "103999" );
xmlDocument.save( "c:/Tests/103999.jdf" );
}
Dwight Kelly
Apago, Inc.
dkelly@apago.com
Replacing values in an XML by a script
Posted: Thu Jul 21, 2011 12:14 pm
by Clive Andrews
Thanks, once again, are in order Dwight - that gives me something to build on!
(",)
Replacing values in an XML by a script
Posted: Wed Jul 27, 2011 11:07 am
by Clive Andrews
dkelly wrote: Clive, the syntax error was caused by missing parentheses.
theNode.replaceChild( theNewTextNode, theNode.getFirstChild() );
However, the code won't work even after you fix this problem because JobID is an attribute not an element. Here's the correct method.
var xmlDocument = new Document( "C:/Tests/101615.jdf" );
if (xmlDocument != null) {
var theElem = xmlDocument.getDocumentElement();
theElem.addAttribute( "JobID", null, "103999" );
xmlDocument.save( "c:/Tests/103999.jdf" );
}
Dwight - still getting errors- "Expected end of statement" on Char 31 - i.e. at end of 1st Bracket (var xmlDocument = new Document()- am I just a bit thick here, it's on a Win server 2003 machine if that helps...
Replacing values in an XML by a script
Posted: Wed Jul 27, 2011 8:58 pm
by dkelly
Clive, I just reran the script and it worked fine here. Can you send me your flow and I'll take a look?
Dwight Kelly
dkelly@apago.com
Replacing values in an XML by a script
Posted: Thu Jul 28, 2011 10:20 am
by Clive Andrews
Ah - therein lies the probable fault, I was kind of hoping to call this from the Apogee machine - I have an AGFA script to call an archive to be recovered, which works manually - I'm trying to tie that into a rename routine, to call an archive back - and then update the files to a current job number. (The Apogee script has to be local as it calls a service)
Classic case of desire exceeding my ability I think!