Powered By Blogger

Friday, July 2, 2010

Exporting table data to a xml doc in Ax using X++

Main thing which is associated with XML in Dynamics AX is the AIF that allows exposing business logic or exchanging data with other external systems. The communication is done by using XML formatted documents. By using existing XML framework application classes prefixed with Axd, you can export or import data from or to the system in an XML format. It is also possible to create new Axd classes using the AIF Document Service Wizard from the Tools Wizards menu to support exporting and importing newly created tables.

Apart from this AX also contains a set of application classes prefixed with Xml like XmlDocument, XmlNode etc to handle the xml import/export which are basically wrappers around the System.

This recipe to show the principle of XML, we will create a new simple XML document by
using the standard xml classes. We will create the file with the data from the chart of accounts table and will save it as an XML file.

just copy the below code and put in a new class named as 'CreateXmlFile' and run it....it will create the xml file at C:\ with the name of 'test'...


public static void main(Args _args)
{
XmlDocument doc;
XmlElement nodeAccount, nodeName, nodeTable, nodeXml;
LedgerTable ledgerTable;
#define.filename("C:\\Test.xml")

;

// Creating a new XmlDocument, which represents an XML structure using its newBlank() method.

doc = XmlDocument::newBlank();

// create it's root node named xml using createElement(), and add the node to the document by calling appendChild().
nodeXml = doc.createElement('xml');
doc.appendChild(nodeXml);

// Query on the table and use the fields which we wanted to include in the xml file.

while select ledgerTable
{
nodeTable = doc.createElement(tablestr(ledgerTable));

nodeTable.setAttribute(fieldstr(LedgerTable, RecId), int642str(ledgerTable.RecId));
nodeXml.appendChild(nodeTable);

nodeAccount = doc.createElement(fieldstr(LedgerTable, AccountNum));
nodeAccount.appendChild(doc.createTextNode(ledgerTable.AccountNum));
nodeTable.appendChild(nodeAccount);

nodeName = doc.createElement(fieldstr(LedgerTable, AccountName));
nodeName.appendChild(doc.createTextNode(ledgerTable.AccountName));
nodeTable.appendChild(nodeName);
}

doc.save(#filename);
}