Freigeben über


Sample of how to write a simple XML document using MSXML DOM in NAV

This post if for when you need to export data in the form of XML from Microsoft Dynamics NAV. If you are not familiar with how to program with MSXML DOM, then it can take a lot of time and work to create even a simple XML document. The purpose of this post is to show a simple example, and to get you started programming with MSXML DOM from NAV.

 I have split this into two posts: "How to write" (this post), and "How to read" an XML Document (click here), since the methods and functions used are quite different.

Why not just use XML-Ports? You can of course, but then you limit yourself to the functionality and limitations that exist in XML-Ports. An example of a limitation in XML-Ports is that they do not support namespaces. If you think that one day you may need functionality that is not supported by XML-Ports, and for which you have to use MSXML DOM anyway, then consider to just use MXSML DOM from the start. It is more complex to begin with, but once you have a set of functions to add / search etc elements in an XML document, then it does not have to be much more complex than using XML-Ports. Note that it is not possible to access an XML docuemnt via MSXML DOM from an XML-Port while the XML-Port is accessing it. The two methods are mutually exclusive.

An XML document is organized as a heararchy of elements, and everything (elements and data) gets added piece by piece to existing elements.

Let's get started with the simplest possible XML DOcument:

Create a new codeunit with 3 variables:
Name     DataType Subtype Length
XMLDoc     Automation 'Microsoft XML, v4.0'.DOMDocument 
DOMNode     Automation 'Microsoft XML, v4.0'.IXMLDOMNode 
DomTextNode     Automation 'Microsoft XML, v4.0'.IXMLDOMText 

In this example I use MSXML version 4, but you can use version 5 or 6 if you prefer. Version 4 is mostly used because it most likely contains all the functionality you will need, and it will be backward compatible to more machines - even old ones.

 

Initialize the document:

OnRun()
CREATE(XMLDoc);
XMLDoc.async(FALSE);

Unless you have specific reasons not to, set asynch = FALSE. Especially when you are reading an XML document. It means that it will load the whole document into memory before it starts reading through it. If you don't set this property, you may begin to process the document before it is completely loaded into memory.

Now create a new node, and then add it to the document:

DOMNode := XMLDoc.createNode(1,'NodeName','');
XMLDoc.appendChild(DOMNode);

Then add some data. This is done in the same way as you add an element: Creat the element first, and then add it to the right place in the document:

DOMTextNode := XMLDoc.createTextNode('NodeValue');
DOMNode.appendChild(DOMTextNode);

You can add any additional elements like this: Creat the element of the type that you need (node, text, attribute etc), and then append it to the document. 

To see the document, save it to disk:
XMLDoc.save('C:\XML\MyXML.xml');

Note: If you run on Vista, it might not allow you to save files on the root of the c: drive. So create a new folder, for example c:\XML\

Then run this codeunit. You should get a very simple document like this:
<NodeName>NodeValue</NodeName>

If you want to add further nodes to this document then repeat the steps above to create a new node, then append it to an existing node. For example, to add a new node:

Create a new variable:
NewDOMNode : 'Microsoft XML, v4.0'.IXMLDOMNode

Initialize the new node:
NewDOMNode := XMLDoc.createNode(1,'NewNode','');
DOMNode.appendChild(NewDOMNode);

And add text to the new node:
DOMTextNode := XMLDoc.createTextNode('NewNodeValue');
NewDOMNode.appendChild(DOMTextNode);

Now, your xml document will look like this:

- <NodeName>

NodeValue

<NewNode>NewNodeValue</NewNode>

</NodeName>

 

Adding namespace and Attributes:

You can add a name space either to the whole document, or to individual nodes. To add a name space to the document, go back to the relevant line above, and specify the 3rd parameter:

DOMNode := XMLDoc.createNode(1,'NodeName','MyNameSpace');

Finally, adding an attribute to a node is similar to adding a text element: Initialize the Attribute, then add it to the relevant node. Use the function SetNamedItem to add attributes. Let's add an attribute called ID with the value 10000:

Create a new Automation variable:
XMLAttributeNode : 'Microsoft XML, v5.0'.IXMLDOMAttribute

Initialize the attribute, then add it to the node:
XMLAttributeNode := XMLDoc.createAttribute('ID');
XMLAttributeNode.value := '10000';
DOMNode.attributes.setNamedItem(XMLAttributeNode);

The whole codeunit should now look like this:

// Initialize the document
CREATE(XMLDoc);
// Create Node and attach it to the document. Use name space MyNameSpacefunction
DOMNode := XMLDoc.createNode(1,'NodeName','MyNameSpace');
XMLDoc.appendChild(DOMNode);

// Add data (text) to the node
DOMTextNode := XMLDoc.createTextNode('NodeValue');
DOMNode.appendChild(DOMTextNode);

// Add an attribute to the node
XMLAttributeNode := XMLDoc.createAttribute('ID');
XMLAttributeNode.value := '10000';
DOMNode.attributes.setNamedItem(XMLAttributeNode);

// Initialize a new node:
NewDOMNode := XMLDoc.createNode(1,'NewNode','');
DOMNode.appendChild(NewDOMNode);

// And add text to the new node:
DOMTextNode := XMLDoc.createTextNode('NewNodeValue');
NewDOMNode.appendChild(DOMTextNode);

// Finally, save the document for viewing:
XMLDoc.save('C:\XML\MyXML.xml');

Further references:

MSXML is further documented in MSXML Software Development Kit (MSXMLSDK). I would recommend that you download this to have the online help available for all of the MSXML DOM functions.

 

These postings are provided "AS IS" with no warranties and confer no rights.
You assume all risk for your use.

 

Lars Lohndorf-Larsen

Microsoft Dynamics UK
Microsoft Customer Service and Support (CSS) EMEA

Comments

  • Anonymous
    March 31, 2008
    This is a follow-up to the post " how to write a simple XML document ". The other post conitains a few

  • Anonymous
    May 21, 2008
    Hin und wieder ist es von Nöten Daten aus Microsoft Dynamics NAV in Form einer XML Datei zu exportieren.

  • Anonymous
    September 01, 2015
    This sample comes out like this: <NodeName xmlns="MyNameSpace" ID="10000">NodeValue<NewNode xmlns="">NewNodeValue</NewNode> </NodeName> My problem is that I want the first namespace (xmlns="MynameSpace") but not the second one xmlns="".  Plus every newly created element will have xmlns="".   How can you get rid of this?