Adding data to a dataset via srcipt

Post Reply
Qpdv
Member
Posts: 20
Joined: Thu Dec 03, 2015 2:59 pm

Adding data to a dataset via srcipt

Post by Qpdv »

Hi,

How can i add data to a dataset when using a script?

I have the following lines of code in my script:

Code: Select all

var databaseDataset = job.getDataset("MPXMLdirect");	
var oldMedia = databaseDataset.getPath("/Log/SQLStatementValue/Row/Column[20]");
is there a databaseDataset.setPath or similar function?

Thanks in advance,

Peter
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Adding data to a dataset via srcipt

Post by gabrielp »

I'm not too familiar with the use of datasets since I rely so much on private data. But I know only embedded datasets can be modified (not external ones). You can check this by using databaseDataset.isWriteable(), which if returns true, can be modified. You can use the setString(), setNumber(), etc.. methods to change stuff. Check page 187 of the scripting guide (I have the version 12 one).
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
bens
Member
Posts: 130
Joined: Thu Mar 03, 2011 10:13 am

Re: Adding data to a dataset via srcipt

Post by bens »

Dataset::getPath returns the path to the dataset backing file on disk, not an XPath in an XML dataset.

To change an existing dataset, you go through a few steps:
1. obtain the dataset, e.g. var databaseDataset = job.getDataset("MPXMLdirect");
2. check the dataset type, e.g. if ( databaseDataset.getModel() == "XML" ) { ... }
3. if it's XML, use the functions in the XML scripting module to update and save the file; be sure to save it to the correct backing file path.

(this is untested pseudo code, don't take it as gospel)

Code: Select all

var databaseDataset = job.getDataset("MPXMLdirect");
if ( databaseDataset.getModel() != "XML" )
{
  job.fail( "oops, not xml" );
}
// it's xml, create an xml Document:
var theXMLDoc = new Document( databaseDataset.getPath() );
// create a new element:
var theNewElement = theXMLDoc.CreateElement( "MyNewElement" );
// add the new element to the xml root:
theXMLDoc.getDocumentElement().appendChild( theNewElement );
// save the file; note that we save to the original dataset path!
theXMLDoc.save( databaseDataset.getPath() );
Post Reply