Page 1 of 1

Adding data to a dataset via srcipt

Posted: Wed Jan 06, 2016 4:58 pm
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

Re: Adding data to a dataset via srcipt

Posted: Wed Jan 06, 2016 5:08 pm
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).

Re: Adding data to a dataset via srcipt

Posted: Fri Jan 08, 2016 10:23 am
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() );