In Navision there are 2 ways to handle XMLs. In all versions it is possible to use the automation Microsoft XML and from Navision 4 it is possible to use XML ports.
XML ports are thought not my preferred object, because they are limited. They work in the same way as a dataport. Therefore in the following we will concentrate us on the use of the automation Microsoft XML.
Before we are able to load/read an XML document, we must define a variable, which will provide us with a container for the document.
Define the variable XMLDom as Microsoft XML – DomDocument
Because we are using automations, we have to create the instant at first:
CREATE(XMLDom);
Now we are ready to load a XML document into the container. There are 2 ways:
1. Loading from a text variable
| cside | | copy code | | ? |
XMLText := '<"1.0" encoding="ISO-8859-1"?>'; |
XMLText += 'SystemStatus'; |
XMLDom.loadXML(XMLText); |
2. Loading from a file
| cside | | copy code | | ? |
XMLFile := 'c:\text.xml'; |
XMLDom.load(XMLFile); |
By the way, the XML file could also be at text stream.
As soon the XML Document has been loaded, we are ready to add tags and read tags.
There are 2 ways to read XMLs. You can parse the XML Document as Nodes or as Elements. Nodes are though often Elements with sub elements. Elements do not contain sub elements.
Tags a common referee to Nodes and Elements
1. Reading as Nodes:
| cside | | copy code | | ? |
XMLNodeList := XMLDoc.getElementsByTagName(Tag); |
Node := XMLNodeList.item(0); |
IF NOT ISCLEAR(Node) THEN |
ReturnValue := Node.text; |
Where XMLNodeList is Microsoft XML IXMLDOMNodeList and Node is Microsoft XML IXMLDOMNode
2. Reading as Elements:
| cside | | copy code | | ? |
Element := XMLDoc.selectSingleNode(TagName); |
IF NOT ISCLEAR(Element) THEN BEGIN |
ReturnValue := Element.text; |
Where Element is Microsoft XML IXMLDOMElement
If you instead of reading wants to build a XML Document you can also do this in 2 ways.
1. Create a XML from a text variable:
| cside | | copy code | | ? |
Building the XML |
XMLText := '<"1.0" encoding="ISO-8859-1"?>'; |
XMLText += ' |
XMLDom.loadXML(XMLText); |
Saving the XML: |
XMLDom.save(xmlfile); |
XML: |
<"1.0" encoding="ISO-8859-1"?> |
|
2. Creating an Init XML and then adding elements /nodes:
| cside | | copy code | | ? |
Build initial XML: |
InitXML := '<"1.0" encoding="ISO-8859-1"?>'+ |
' |
XMLDom.load(InitXML); |
Add Element: |
Root := XMLdoc.documentElement; |
NewTag := XMLDom.createElement('Type');
|
NewTag.text := 'SystemStatus'; |
Root.appendChild(NewTag); |
XML: |
<"1.0" encoding="ISO-8859-1"?> |
|