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
-
- Newbie
- Posts: 16
- Joined: Mon Apr 04, 2011 3:56 pm
Replacing values in an XML by a script
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
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

-
- Newbie
- Posts: 16
- Joined: Mon Apr 04, 2011 3:56 pm
Replacing values in an XML by a script
Hi Bens,
thanks for the quick reply, it's working perfectly!
Kind regards,
Peter
thanks for the quick reply, it's working perfectly!
Kind regards,
Peter
-
- Member
- Posts: 85
- Joined: Thu Jun 23, 2011 11:41 am
Replacing values in an XML by a script
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!
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!
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
Replacing values in an XML by a script
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
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
-
- Member
- Posts: 85
- Joined: Thu Jun 23, 2011 11:41 am
Replacing values in an XML by a script
Thanks, once again, are in order Dwight - that gives me something to build on!
(",)
(",)
-
- Member
- Posts: 85
- Joined: Thu Jun 23, 2011 11:41 am
Replacing values in an XML by a script
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...
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...
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
Replacing values in an XML by a script
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
Dwight Kelly
dkelly@apago.com
-
- Member
- Posts: 85
- Joined: Thu Jun 23, 2011 11:41 am
Replacing values in an XML by a script
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!
Classic case of desire exceeding my ability I think!