Tuesday, 10 March 2015

File Handling in AX 2012

Writing to a Text File

static void textFileWrite(Args _args)
{
TextBuffer tb = new TextBuffer();
;
tb.appendText(“Hello World!”);
tb.appendText(“\nWelcome to MIT”);
tb.appendText(“\nHyderabad”);
tb.toFile(“c:\\Sample.txt”);
}
Reading from a Text File

static void textFileRead(Args _args)
{
TextBuffer tb = new TextBuffer();
;
if(WinApi::fileExists(“c:\\Sample.txt”))
{
tb.fromFile(“c:\\Sample.txt”);
info(tb.getText());
}
else
{
info(“No file exists”);
}
}
Writing to a CSV File

static void cSVFileWrite(Args _args)
{
CommaIO commaIO;
Container readCon;
FileIOPermission fio;
FileDemo ct;
;
commaIO = new CommaIO(“c:\\FileDemo.csv”, “w”);
commaIO.outFieldDelimiter(“,”);
commaIO.outRecordDelimiter(“\r\n”);
if(commaIO)
{
while select ct
{
commaIO.write(ct.Id,ct.Name);
}
}
}

Reading from a CSV File

static void cSVFileRead(Args _args)
{
CommaIO commaIO;
Container readCon;
FileIOPermission fio;
FileDemo ct;
;
if(!WinAPI::fileExists(“c:\\FileDemo.csv”))
{
throw error(“File not available”);
}
fio = new FileIOPermission(“c:\\FileDemo.csv”, “r”);
fio.assert();
commaIO = new CommaIO(“c:\\FileDemo.csv”, “r”);
commaIO.inFieldDelimiter(“,”);
commaIO.inRecordDelimiter(“\r\n”);
if(commaIO)
{
while(CommaIO.status() == IO_Status::Ok)
{
readCon = commaIO.read();
if(conlen(readcon) > 0)
{
ct.Id = conpeek(readcon, 1);
ct.Name = conpeek(readcon, 2);
ct.insert();
}
}
}
}

Writing to a XML File

static void writeXML(Args _args)
{
XMLTextWriter xw;
FileDemo ct;
;
xw = XMLTextWriter::newFile(“c:\\FileDemo.xml”);
xw.writeStartDocument();
xw.writeStartElement(“Details”);
while select ct
{
xw.writeStartElement(“CustomerDetails”);
xw.writeElementString(“Id”,ct.Id);
xw.writeElementString(“Name”,ct.Name);
xw.writeEndElement();
}
xw.writeEndElement();
xw.writeEndDocument();
xw.close();
}

Reading from a XML File

static void readingXML(Args _args)
{
XMLDocument xmlDoc = XMLDocument::newFile(“c:\\FileDemo.xml”);
int i, nooftags;
;
nooftags = xmlDoc.getElementsByTagName(“CustomerDetails”).length();
for(i=0; i<nooftags; i++)
{
info(strfmt(“%1″, xmlDoc.getElementsByTagName(“Id”).item(i).text()));
info(strfmt(“%1″, xmlDoc.getElementsByTagName(“Name”).item(i).text()));
}
}

TableData_to_File


static void TableData_to_File(Args _args)
{
commaIO commaIO;
container readLine;
CustTable k;
FileIOPermission fio;
FileName nameOfTheFile = “c:\\anil.txt”; //csv
;
fio = new FileIOPermission(nameOfTheFile, ‘w’);// read – r, write – w, append – a
fio.assert(); // demand
commaIO = new commaIO(nameOfTheFile,’w');
commaIO.outFieldDelimiter(‘,’); // \t | // outFieldDelimiter
commaIo.outRecordDelimiter(‘\r\n’); // OutRecorDelimiter
commaIO.writeExp(["Accno", "AccountStatement", "CommissionGroup","DefaultDimention"]);
while select k
{
commaIO.writeExp([k.AccountNum,
k.AccountStatement,k.CommissionGroup,k.DefaultDimension]);
}
CodeAccessPermission::revertAssert();
}

TableDataFrom_File


static void TableDataFrom_File(Args _args)
{
commaIO commaIO;
container readLine;
Tab2 k;
FileIOPermission fio;
FileName nameOfTheFile = “c:\\anil.txt”; //csv
;
if (winapi::fileExists(nameOfTheFile) == false)
throw error(strfmt(“file %1 not found”, nameOfTheFile) );
fio = new FileIOPermission(nameOfTheFile, ‘r’);// read – r, write – w, append – a
fio.assert(); // demand
commaIO = new commaIO(nameOfTheFile,’r');
commaIO.inFieldDelimiter(‘,’); // \t – tab, | // outFieldDelimiter
commaIo.inRecordDelimiter(‘\r\n’); // OutRecorDelimiter – read line by line
ttsbegin;
if(commaIO)
{
while(commaIO.status() == IO_status::Ok)
{
readLine = commaIO.read(); //writeExp
if (conlen(readLine) > 1) // check whether there are values in the next line
{
k.Address = conpeek(readLine,1);
k.DName = conpeek(readLine, 2);
k.insert();
}
}
}
ttscommit;


CodeAccessPermission::revertAssert();
}

No comments:

Post a Comment