Freigeben über


How to read from NAV xml document using Microsoft XMLDOM automation

Usually i meet questions about "How to read from NAV xml documents without xmlports?"
It is because xml ports have some limitations and sometimes don't allow us to use it.
There is sample codeunit which reads xml nodes from file. It is just example and no more....:)

OBJECT Codeunit 50050 xml read
{
  OBJECT-PROPERTIES
  {
    Date=05.08.12;
    Time=15:13:32;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            ffile.OPEN('C:\XmlFile.xml'); //this must be your file name
            ffile.CREATEINSTREAM(strInStream);

            IF ISCLEAR(xmldomDoc) THEN CREATE(xmldomDoc);

            xmldomDoc.load(strInStream);

            xmlNodeList1:= xmldomDoc.childNodes();

            FOR i:=1 TO xmlNodeList1.length()-1 DO BEGIN
             xmldomElem1:= xmlNodeList1.item(i);
                IF NOT ISCLEAR(xmldomElem1) THEN
                   xmldomAttrib:= xmldomElem1.getAttributeNode('ID');
                   IF NOT ISCLEAR(xmldomAttrib) THEN
                      txtid:=xmldomAttrib.value();
                   xmldomAttrib:= xmldomElem1.getAttributeNode('Type');
                   IF NOT ISCLEAR(xmldomAttrib) THEN
                      txttype:=xmldomAttrib.value();
                   xmldomAttrib:= xmldomElem1.getAttributeNode('Version');
                   IF NOT ISCLEAR(xmldomAttrib) THEN
                      txtVer:=xmldomAttrib.value();

                  //>>Find Node next level
                     xmldomElem2:=xmldomElem1.firstChild();
                     xmldomElem3:=xmldomElem2.firstChild();
                     xmldomAttrib:= xmldomElem3.getAttributeNode('Author');
                      IF NOT ISCLEAR(xmldomAttrib) THEN
                         txtAut:=xmldomAttrib.value();
                     xmldomAttrib:= xmldomElem3.getAttributeNode('TimeStamp');
                      IF NOT ISCLEAR(xmldomAttrib) THEN
                         txtTS:=xmldomAttrib.value();

                  //Next node

                  xmlNodeList2:= xmldomElem1.childNodes();
                  FOR ii:=0 TO xmlNodeList2.length()-2 DO BEGIN
                     xmldomElem2:= xmlNodeList2.item(ii);
                     xmldomElem3:= xmldomElem2.firstChild();

                   xmldomAttrib:= xmldomElem3.getAttributeNode('CustomerID');
                   IF NOT ISCLEAR(xmldomAttrib) THEN
                      txtCI:=xmldomAttrib.value();
                   xmldomAttrib:= xmldomElem3.getAttributeNode('ID');
                   IF NOT ISCLEAR(xmldomAttrib) THEN
                      txtCID:=xmldomAttrib.value();

                   //go deeper
                   xmldomElem3:= xmldomElem3.firstChild();

                   IF NOT ISCLEAR(xmldomElem3) THEN
                   xmldomAttrib:= xmldomElem3.getAttributeNode('OrganizationName');
                   IF NOT ISCLEAR(xmldomAttrib) THEN
                      txtON:=xmldomAttrib.value();

 

                  END;
                  MESSAGE('ID "%1"\Type "%2"\Version "%3"\Author "%4"\timestamp "%5"\Customer ID "%6"\Contact ID "%7"\Org Name "%8"',
                  txtid,
                  txttype,
                  txtVer,
                  txtAut,
                  txtTS,
                  txtCI,
                  txtCID,
                  txtON);

            END;
            ffile.CLOSE;
            CLEARALL;
          END;

  }
  CODE
  {
    VAR
      xmldomDoc@1000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{88D969E5-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v5.0'.DOMDocument50";
      xmlNodeList1@1005 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{2933BF82-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v5.0'.IXMLDOMNodeList";
      xmlNodeList2@1017 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{2933BF82-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v5.0'.IXMLDOMNodeList";
      xmldomElem1@1007 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{2933BF86-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v5.0'.IXMLDOMElement";
      xmldomElem2@1010 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{2933BF86-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v5.0'.IXMLDOMElement";
      xmldomElem3@1011 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{2933BF86-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v5.0'.IXMLDOMElement";
      xmldomAttrib@1008 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 5.0:{2933BF85-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v5.0'.IXMLDOMAttribute";
      txtid@1001 : Text[30];
      txttype@1002 : Text[30];
      txtVer@1009 : Text[30];
      txtAut@1012 : Text[30];
      txtTS@1013 : Text[30];
      txtCI@1014 : Text[30];
      txtCID@1015 : Text[30];
      txtON@1016 : Text[30];
      ffile@1003 : File;
      strInStream@1004 : InStream;
      i@1006 : Integer;
      ii@1018 : Integer;

    EVENT <xmldomDoc@1000::ondataavailable@198>();
    BEGIN
    END;

    EVENT <xmldomDoc@1000::onreadystatechange@-609>();
    BEGIN
    END;

    BEGIN
    END.
  }
}

 

Questions like "How to create xml files from NAV" are more easier, because you can use xmlport, dataport, write directly to file or use the same xmlDom automation.