Pages

Monday, 8 June 2015

Reading AIF outbound xml with multiple child nodes via jobs in dynamics ax 2009

static void XMLReadList(Args _args)
{
    XMLDocument     doc;
    XMLNode         rootNode,NodeAccount;
    XMLNodeList     xmlRootNodeList;
    XmlNodeList     xmlRecordList;
    XMLParseError   xmlError;
    XmlElement      xmlBodyField,xmlMessageField,xmlbbbField,xmlCustTableField;
    XmlNodeList     xmlBodyFieldList,xmlMessageBodyChildFieldList,xmlbbbaxdcustomerChildFieldList,xmlCustTableFieldList,xmlDirTableFieldList,xmlAddFieldList,xmlDirFieldList;
    XmlElement      xmlRecord,xmlMessageChildRecord,xmlbbbaxdcustomerChildRecord,xmlCustTableRecord,xmlDirTableRecord,xmlAddRecord;
    str             custAccount,cellularPhone,phone,email,street,state,city,zipcode,fname,lname,bbb_birthday,bbb_spousename,birthdate,anniversaryDate;
    int             i,j,k,l,m,n,p,fieldId;
    CustTable       custTable;

    ;
    // Read the XML Document
    doc = new XMLDocument();
    //doc.async(FALSE);
    doc.load(@"E:\\demo.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();
    xmlRootNodeList = doc.documentElement().selectNodes("//Envelope");
    // Get all Customer information from XML document tags (and remove it)
    NodeAccount = doc.getNamedElement('Envelope');
    xmlRootNodeList = NodeAccount.childNodes();
    if(doc.getElementsByTagName('CustTable').length() > 0)
    {
        for(i=0;i<xmlRootNodeList.length();i++)
        {
            xmlRecord = xmlRootNodeList.item(i);
            if(i==1)
            {
                xmlBodyFieldList = xmlRecord.childNodes();

                for (j=0; j<xmlBodyFieldList.length(); j++)
                {
                    xmlBodyField = xmlBodyFieldList.item(j);
                    xmlMessageBodyChildFieldList = xmlBodyField.childNodes();

                    for(k=0;k<xmlMessageBodyChildFieldList.length();k++)
                    {
                        xmlMessageChildRecord = xmlMessageBodyChildFieldList.item(k);
                        xmlbbbaxdcustomerChildFieldList = xmlMessageChildRecord.childNodes();
                        if(xmlbbbaxdcustomerChildFieldList.length() > 0)
                        {
                            for(j=0;j<xmlbbbaxdcustomerChildFieldList.length();j++)
                            {
                                xmlbbbaxdcustomerChildRecord = xmlbbbaxdcustomerChildFieldList.item(j);
                                xmlCustTableFieldList = xmlbbbaxdcustomerChildRecord.childNodes();
                                custAccount      = xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('AccountNum').text();


                                if(xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('CellularPhone'))
                                {
                                    cellularPhone    = xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('CellularPhone').text();
                                }
                                if(xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('Email'))
                                {
                                    email            = xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('Email').text();
                                }
                                if(xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('Phone'))
                                {
                                    phone = xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('Phone').text();
                                }
                                xmlDirTableFieldList = xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('DirPartyTable').childNodes();
                                fname ='';
                                lname ='';
                                for(m=0;m<xmlDirTableFieldList.length();m++)
                                {
                                    if( xmlDirTableFieldList.item(m).baseName() == 'FirstName')
                                    {
                                        fname = xmlDirTableFieldList.item(m).text();
                                    }
                                    if( xmlDirTableFieldList.item(m).baseName() == 'LastName')
                                    {
                                        lname = xmlDirTableFieldList.item(m).text();
                                    }
                                    xmlDirFieldList = xmlDirTableFieldList.item(m).childNodes();

                                    birthday = '0000-00-00 00:00:00';
                                    spousename = '';
                                    birthdate = '0000-00-00 00:00:00';
                                    AnniversaryDate = '0000-00-00 00:00:00';
                                    for(p=0;p<xmlDirFieldList.length();p++)
                                    {

                                         if(xmlDirFieldList.item(p).baseName()=='Spouse_Birthday')
                                         {
                                            birthday = xmlDirFieldList.item(p).text();
                                         }

                                         if(xmlDirFieldList.item(p).baseName()=='Spouse_Name')
                                         {
                                            spousename = xmlDirFieldList.item(p).text();
                                         }

                                         if(xmlDirFieldList.item(p).baseName()=='BirthDate')
                                         {
                                            birthdate = xmlDirFieldList.item(p).text();
                                         }

                                         if(xmlDirFieldList.item(p).baseName()=='AnniversaryDate')
                                         {
                                            anniversaryDate = xmlDirFieldList.item(p).text();
                                         }
                                    }

                                }
                                xmlAddFieldList = xmlbbbaxdcustomerChildFieldList.item(j).getNamedElement('Address_1').childNodes();
                                city    ='';
                                state   ='';
                                street  ='';
                                zipcode ='';
                                for(n=0;n<xmlAddFieldList.length();n++)
                                {
                                    if(xmlAddFieldList.item(n).baseName() == 'City')
                                    {
                                        city = xmlAddFieldList.item(n).text();
                                    }
                                    if(xmlAddFieldList.item(n).baseName() == 'State')
                                    {
                                        state = xmlAddFieldList.item(n).text();
                                    }
                                    if(xmlAddFieldList.item(n).baseName() == 'Street')
                                    {
                                        street = xmlAddFieldList.item(n).text();
                                    }
                                    if(xmlAddFieldList.item(n).baseName() == 'ZipCode')
                                    {
                                        zipcode = xmlAddFieldList.item(n).text();
                                    }
                                }
                            }
                        }
                        else
                        {
                            info("Xml File is Empty");
                        }
                    }
                }
            }
        }
     }
     else
     {
        info("Xml File is Empty");
     }
}

No comments:

Post a Comment