Page 1 of 1
Loop through XML, find assets
Posted: Thu Feb 04, 2016 8:48 pm
by StoereSjoerd
Hi,
I have the following assets stored into a folder:
Asset-1.pdf
Asset-2.pdf
Asset-3.pdf
Within a XML there are nodes with values regarding to these assets:
Code: Select all
<FileName>Asset-1.pdf</FileName>
<FileName>Asset-2.pdf</FileName>
<FileName>Asset-3.pdf</FileName>
So I want to collect these assets and archive them based on the data from the XML. How can this be accomplished? I think scripting is necessary in this case.
Regards, Sjoerd
Re: Loop through XML, find assets
Posted: Fri Feb 05, 2016 3:21 am
by gabrielp
You could use a script for this but maybe give this a shot
http://forum.enfocus.com/viewtopic.php?f=12&t=1201
Re: Loop through XML, find assets
Posted: Fri Feb 05, 2016 6:48 am
by StoereSjoerd
Thank you for your quick response.
But splitting the XML is losing the coherence of the assets. I need to combine these assets into one job for archiving it afterwards. I think the 'asssemble job' configurator needs to do the job. But my script which is looping through the XML finding the assets, does not do the desired job:
Code: Select all
var myAssets = job.getVariableAsString('[Metadata.Text:Path="/Order/Assets/FileName",Dataset="Collect",Model="XML"]');
for (var i=0; i<myAssets.length; i++) {
myAssets[i];
}
Re: Loop through XML, find assets
Posted: Fri Feb 05, 2016 11:53 am
by loicaigon
Hi,
I am guessing your issues is that your presume myAssets is an array like. Infact, it's a String made of the first occurence of FileName.
When you loop through myAssets length, you are actually working with every single character of the string. If myAssets is "a.pdf", you will process "a", then"." then "p" then "d" then "f".
Here is my proposal, rather than looking into the metadata and given that the xml file is the job, I suggest to load a document and look through nodes :
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 )
{
var d = new Document ( job.getPath() );
var f, url, j, displaced = 0;
var elems = d.evalToNodes("/Order/Assets/FileName");
var n = elems.getCount( );
while (n--) {
url = "/Users/ozalto/Desktop/"+elems.getItem(n).evalToString(".");
if ( File.exists ( url ) ) {
s.log(-1,"File found" );
f = new File ( url );
j = s.createNewJob(url);
j.sendToSingle(url);
displaced++;
}
else {
s.log(3, "File not found "+url );
}
}
if ( displaced == n ) job.sendToNull();
else job.fail();
}
HTH,
Loic
http://www.ozalto.com
Re: Loop through XML, find assets
Posted: Sat Feb 06, 2016 4:26 pm
by StoereSjoerd
Hi Loic,
Thank you for your example. This script is picking up the assets regarding the XML indeed. But how to collect these assets to one job or archive file?
Regards, Sjoerd
Re: Loop through XML, find assets
Posted: Mon Feb 08, 2016 3:23 pm
by StoereSjoerd
The following script is sending all assets separtly to the connected folder:
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 )
{
var theXML = new Document(job.getPath());
var nodeList = theXML.evalToNodes("/Order/Versions/Version1/Assets/FileName");
for (var i=0; i < nodeList.length; i++) {
var theNode = nodeList.getItem(i);
var theAssets = "/Users/Administrator/Desktop/Test Flows/Meerdere/Assets/"+theNode.evalToString(".");
job.log(1, "Found assets: "+theNode.evalToString("."));
job = s.createNewJob(theAssets);
job.sendToSingle(theAssets);
}
}
Now I want to collect these and store them within a folder for archiving it into one zip file afterwards. I tried following script with no luck
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 )
{
var theXML = new Document(job.getPath());
var nodeList = theXML.evalToNodes("/Order/Versions/Version1/Assets/FileName");
var theDir = new Dir( "/Storage/" );
var theTempPath = s.createPathWithName(theDir);
job = s.createNewJob(theTempPath);
for (var i=0; i < nodeList.length; i++) {
var theNode = nodeList.getItem(i);
var theAssets = "/Users/Administrator/Desktop/Test Flows/Meerdere/Assets/"+theNode.evalToString(".");
job.log(1, "Found assets: "+theNode.evalToString("."));
job.sendToSingle(theTempPath, theAssets);
}
}
This script isn't sendig files to connected folder at all. How can this be accomplished?
Re: Loop through XML, find assets
Posted: Mon Feb 08, 2016 7:51 pm
by gabrielp
I'm a little confused about your code, so take what I'll say with a grain of salt. But a couple issues I'm noticing here. You're overwriting your job variable. Switch gives you "job" which is it's Job class instantiated with the current job which triggered jobArrived(), so overwriting that is probably not what you want to do.
Secondly, once you have a new job, what are you doing to it? Usually when you create a new job, you get a writable path with something like job.createPathWithName which returns a path you can write to, once complete, sending the job out with a sendTo function.