jeudi 1 mars 2012

XML file parsing


    Within Dynamics you have a module called the application integration 
    framework which allows you to trade XML messages electronically.
    Firstly, identify whether this is available via licence.. options to 
    configure AIF for document exchange within the BASIC folder.

    Unfortunately, cannot seem to attach documents otherwise would send
    you the instructions.. Can also build XML documents manually using
    below functionality..

    To create an XML file in Microsoft Dynamics AX, follow these steps:
    1. Create a new job. To do this, click Open on the File menu,
    and then click Application Object Tree. Right-click Jobs, and then
    click New Job.
    2. In the editor, type the following code:
    static void XMLWriteVendorList(Args _args)
    {
    XMLDocument xmlDoc = XMLDocument::newBlank();
    XMLNode rootNode;
    XMLNode
    NodeVend,NodeName,NodeAddr,NodeCurrency,NodeCredit,NodeAmount,NodeTran
    sDate,NodeDueDate;
    VendTable vendTable;
    VendTransOpen vendtransOpen;
    DialogButton db;
    ;

    rootNode = xmlDoc.documentElement();
    rootNode = xmlDoc.appendChild(xmlDoc.createElement('VendorList'));

    db = Box::yesNo('Create VendorList.xml in C/
    \:',Dialogbutton::Yes,'Specify an option');

    if (db == DialogButton::Yes)
    {
    while select vendTable
    join vendtransOpen
    where vendTable.AccountNum==vendtransOpen.AccountNum
    {
    NodeVend = rootNode.appendChild(xmlDoc.createElement
    ('Vendor'));

    NodeName = NodeVend.appendChild(xmlDoc.createElement
    ('VendName'));
    NodeName.appendChild(xmlDoc.createTextNode
    (vendTable.Name));

    NodeAddr = nodeVend.appendChild(xmlDoc.createElement
    ('VendAddr'));
    NodeAddr.appendChild(xmlDoc.createTextNode
    (vendTable.Address));

    NodeCurrency = NodeVend.appendChild(xmlDoc.createElement
    ('VendCurr'));
    NodeCurrency.appendChild(xmlDoc.createTextNode
    (vendTable.Currency));

    NodeCredit = NodeVend.appendChild(xmlDoc.createElement
    ('VendCredit'));
    NodeCredit.appendChild(xmlDoc.createTextNode(Num2str
    (vendTable.CreditMax,10,2,1,2)));

    NodeAmount = NodeVend.appendChild(xmlDoc.createElement
    ('VendAmountCurr'));
    NodeAmount.appendChild(xmlDoc.createTextNode(Num2str
    (vendtransOpen.AmountCur,10,2,1,2)));

    NodeTransDate = NodeVend.appendChild(xmlDoc.createElement
    ('VendTransDate'));
    NodeTransDate.appendChild(xmlDoc.createTextNode(date2str
    (vendtransOpen.TransDate,123,2,2,2,2,4)));

    NodeDueDate = NodeVend.appendChild(xmlDoc.createElement
    ('VendDueDate'));
    NodeDueDate.appendChild(xmlDoc.createTextNode(date2str
    (vendtransOpen.DueDate,123,2,2,2,2,4)));
    }
    xmldoc.save('c:\VendorList.xml');
    Box::info('File created in C:','Operation Finished');
    }
    else if (db==DIalogbutton::No)
    {
    Box::warning('You chose to NOT create the file','Operation
    aborted');
    }
    }
    3. Run this job in Microsoft Dynamics AX. When you do this, the
    following VendorList.xml file is generated:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <VendorList>
    <Vendor>
    <VendName> Coho Winery </VendName>
    <VendAddr>1234 W. 5th Ave., Lion, GA 34589</VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>1,500.00</VendCredit>
    <VendAmountCurr>-7,250.00</VendAmountCurr>
    <VendTransDate>11.01.2002</VendTransDate>
    <VendDueDate>15.02.2002</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName> Coho Winery </VendName>
    <VendAddr>1234 W. 5th Ave., Lion, GA 34589 </VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>1,500.00</VendCredit>
    <VendAmountCurr>-5.63</VendAmountCurr>
    <VendTransDate>18.12.2006</VendTransDate>
    <VendDueDate>15.01.2007</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName> Coho Winery </VendName>
    <VendAddr>1234 W. 5th Ave., Lion, GA 34589 </VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>1,500.00</VendCredit>
    <VendAmountCurr>9,748.75</VendAmountCurr>
    <VendTransDate>18.12.2006</VendTransDate>
    <VendDueDate>15.01.2007</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName> Northwind Traders </VendName>
    <VendAddr>456 Main Ave., Robert, MN 98769</VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>0.00</VendCredit>
    <VendAmountCurr>-4.95</VendAmountCurr>
    <VendTransDate>21.12.2006</VendTransDate>
    <VendDueDate>15.01.2007</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName>Tailspin Toys </VendName>
    <VendAddr>789 Martin St., Smithtown, GA 31095</VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>0.00</VendCredit>
    <VendAmountCurr>9,858.75</VendAmountCurr>
    <VendTransDate>22.12.2006</VendTransDate>
    <VendDueDate>15.01.2007</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName>Woodgrove Bank</VendName>
    <VendAddr>4567 Main St., Buffalo, NY 98052</VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>3,500.00</VendCredit>
    <VendAmountCurr>-2,000,000.00</VendAmountCurr>
    <VendTransDate>01.01.2002</VendTransDate>
    <VendDueDate>02.03.2002</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName> Woodgrove Bank </VendName>
    <VendAddr>4567 Main St., Buffalo, NY 98052</VendAddr>
    <VendCurr>USD</VendCurr>
    <VendCredit>3,500.00</VendCredit>
    <VendAmountCurr>-20,000.00</VendAmountCurr>
    <VendTransDate>01.01.2002</VendTransDate>
    <VendDueDate>02.03.2002</VendDueDate>
    </Vendor>
    <Vendor>
    <VendName> Lucerne Publishing</VendName>
    <VendAddr>456 North St., Richardville, Canada A1B 2C3</VendAddr>
    <VendCurr>CAD</VendCurr>
    <VendCredit>0.00</VendCredit>
    <VendAmountCurr>-55.38</VendAmountCurr>
    <VendTransDate>13.03.2002</VendTransDate>
    <VendDueDate>30.04.2002</VendDueDate>
    </Vendor>
    </VendorList>
    How to read an XML file
    You can use the following methods to read XML files in Microsoft
    Dynamics AX:
    • Read the XML file line by line. When you do this, you can
    confirm that the correct tags are used for the data.
    • Use the microsoft.xmldom COM object. This method is the
    easiest method.
    To use the microsoft.xmldom COM object to read an XML file that is
    saved in drive C that is named "VendorList.xml", follow these steps:
    1. Create a job. To do this, click Open on the File menu, and
    then click Application Object Tree. Right-click Jobs, and then click
    New Job.
    2. In the editor, type the following sample code:
    static void XMLReadVendorList(Args _args)
    {
    XMLDocument doc;
    XMLNode
    rootNode,NodeVend,NodeName,NodeAddr,NodeCurrency,NodeCredit,NodeAmount
    ,NodeTransDate,NodeDueDate;
    XMLParseError xmlError;
    int i;

    // Read the XML Document
    doc = new XMLDocument();
    //doc.async(FALSE);
    doc.load("C:\\VendorList.xml");

    // Verify Document Structure
    xmlError = doc.parseError();

    if(xmlError && xmlError.errorCode() != 0)
    {
    throw error(strFmt("Error: %1",xmlError.reason()));
    }

    // Parsing document contents
    rootNode = doc.documentElement();

    // Get all Vendor information from XML document tags (and remove
    it)
    NodeVend = rootNode.selectSingleNode("//Vendor");
    NodeName = rootNode.selectSingleNode("//VendName");
    NodeAddr = rootNode.selectSingleNode("//VendAddr");
    NodeCurrency = rootNode.selectSingleNode("//VendCurr");
    NodeCredit = rootNode.selectSingleNode("//VendCredit");
    NodeAmount = rootNode.selectSingleNode("//VendAmountCurr");
    NodeTransDate = rootNode.selectSingleNode("//VendTransDate");
    NodeDueDate = rootNode.selectSingleNode("//VendDueDate");

    // From here you can use the retrieved information to insert the
    data into a temporary table or
    // process the data to obtain results
    // Print gathered information from XML file
    info(rootNode.text());
    }
    3. Run this job in Microsoft Dynamics AX. This code reads the
    VendorList.xml file that is stored in drive C and then displays the
    data in an information box. You can do any of the following tasks
    with the data:
    • Insert this data into a temporary table.
    • Process this data to obtain results.
    • Print the data.

Aucun commentaire:

Enregistrer un commentaire