Thursday, July 2, 2015

Exporting data to an XML file

class CreateXmlFile
{
}


public static void main(Args _args)
{
XmlDocument doc;
XmlElement nodeXml;
XmlElement nodeTable;
XmlElement nodeAccount;
XmlElement nodeName;
MainAccount mainAccount;
#define.filename(@'C:\Users\XYZ\Desktop\AX(FolderName)\accounts.xml')
doc = XmlDocument::newBlank();  /*----Create a new XML Document------*/
nodeXml = doc.createElement('xml'); /*----Create its root node named xml using the createElement() method ------*/
doc.appendChild(nodeXml); /*-----add the node to the document by calling the document's
appendChild() method. ------*/
while select RecId, MainAccountId, Name from mainAccount
{
nodeTable = doc.createElement(tableStr(MainAccount));
nodeTable.setAttribute(fieldStr(MainAccount, RecId),int642str(mainAccount.RecId));
nodeXml.appendChild(nodeTable);
nodeAccount = doc.createElement(
fieldStr(MainAccount, MainAccountId));
nodeAccount.appendChild(
doc.createTextNode(mainAccount.MainAccountId));
nodeTable.appendChild(nodeAccount);
nodeName = doc.createElement(
fieldStr(MainAccount, Name));
nodeName.appendChild(
doc.createTextNode(mainAccount.Name));
nodeTable.appendChild(nodeName);
}
doc.save(#filename);
info(strFmt("File %1 created.", #filename));
}

/* Explaination for the code */

Next, we go through the MainAccount table and do the following for each record:
1. Create a new XmlElement node, which is named exactly as the table name, and add
this node to the root node.
2. Create a node representing the account number field and its child node representing
its value. The account number node is created using createElement(), and its
value is created using createTextNode(). The createTextNode() method
basically adds a value as text with no XML tags.
3. Add the account number node to the table node.
4. Create a node representing the account name field and its child node representing
its value.
5. Add the account name node to the table node.
Finally, we save the created XML document as a file.

No comments:

Post a Comment

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// ...