X++ Code to read XML file in D365FO
X++ Code to read XML file in D365FO
Hi Guys,
The below code will help you to read an XML file from Local File Path and insert into Table.
XmlDocument xmlDocument;
XMLParseError xmlError;
XmlNode rootNode,bodyNode;
XMLNodeList tagsList;
XmlNamedNodeMap map;
int j;
RecordInsertList recordInsertListCust;
CustomerTableTmp customerTabletmp;
Str fileName =@''D:Temp\customer.XML";
xmlDocument = new XmlDocument();
xmlDocument.load(fileName);
xmlError = xmlDocument.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
throw error(strFmt("XML Error: %1", xmlError.reason()));
}
rootNode = xmlDocument.documentElement();
recordInsertListCust = new RecordInsertList(tableNum(CustomerTableTmp));
if(rootnode.hasChildNodes())
{
tagsList = rootnode.childNodes();
for(j = 0; j < tagsList.length(); j++)
{
bodyNode = tagsList.item(j);
map= bodyNode.attributes();
customerTabletmp.clear();
customerTabletmp.Account = map.nextNode().nodeValue();
customerTabletmp.Address = map.nextNode().nodeValue();
customerTabletmp.ContactPersonNo= map.nextNode().nodeValue();
customerTabletmp.Country = map.nextNode().nodeValue();
customerTabletmp.Currency = map.nextNode().nodeValue();
customerTabletmp.CustomerGroup =map.nextNode().nodeValue();
customerTabletmp.Discount =map.nextNode().nodeValue();
customerTabletmp.Email =map.nextNode().nodeValue();
customerTabletmp.GSTNo =map.nextNode().nodeValue();
customerTabletmp.GSTType =map.nextNode().nodeValue();
customerTabletmp.Name = map.nextNode().nodeValue();
customerTabletmp.PANNumber =map.nextNode().nodeValue();
customerTabletmp.State =map.nextNode().nodeValue();
customerTabletmp.Imported = NoYes::No;
recordInsertListCust.add(customerTabletmp);
}
recordInsertListCust.insertDatabase();
}
Hi Guys,
The below code will help you to read an XML file from Local File Path and insert into Table.
XmlDocument xmlDocument;
XMLParseError xmlError;
XmlNode rootNode,bodyNode;
XMLNodeList tagsList;
XmlNamedNodeMap map;
int j;
RecordInsertList recordInsertListCust;
CustomerTableTmp customerTabletmp;
Str fileName =@''D:Temp\customer.XML";
xmlDocument = new XmlDocument();
xmlDocument.load(fileName);
xmlError = xmlDocument.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
throw error(strFmt("XML Error: %1", xmlError.reason()));
}
rootNode = xmlDocument.documentElement();
recordInsertListCust = new RecordInsertList(tableNum(CustomerTableTmp));
if(rootnode.hasChildNodes())
{
tagsList = rootnode.childNodes();
for(j = 0; j < tagsList.length(); j++)
{
bodyNode = tagsList.item(j);
map= bodyNode.attributes();
customerTabletmp.clear();
customerTabletmp.Account = map.nextNode().nodeValue();
customerTabletmp.Address = map.nextNode().nodeValue();
customerTabletmp.ContactPersonNo= map.nextNode().nodeValue();
customerTabletmp.Country = map.nextNode().nodeValue();
customerTabletmp.Currency = map.nextNode().nodeValue();
customerTabletmp.CustomerGroup =map.nextNode().nodeValue();
customerTabletmp.Discount =map.nextNode().nodeValue();
customerTabletmp.Email =map.nextNode().nodeValue();
customerTabletmp.GSTNo =map.nextNode().nodeValue();
customerTabletmp.GSTType =map.nextNode().nodeValue();
customerTabletmp.Name = map.nextNode().nodeValue();
customerTabletmp.PANNumber =map.nextNode().nodeValue();
customerTabletmp.State =map.nextNode().nodeValue();
customerTabletmp.Imported = NoYes::No;
recordInsertListCust.add(customerTabletmp);
}
recordInsertListCust.insertDatabase();
}
Limitation:The way XML attribute and values are arranged in XML file,in the same order we need to insert to table.
ThankYou.
Comments
Post a Comment