Promotic
WikipediaLinkedInYoutubeTwitterFacebook

How to read common XML files in the application

These examples describe how to read specific values or parameters into the PROMOTIC application from XML file using the CreateObject method via Microsoft.XMLDOM.

The example for creating (writing) the XML file is not available here. It is also possible to use the methods over the Microsoft.XMLDOM object, but in most cases it is much easier to create the XML file as a text fule by using the Pm.FileTextWrite method (the whole XML text is first created in a variable of the String type) or Pm.FileTextReplace (the template of the XML file is overwritten with the keywords replaced).

XmlData:
Create the example file named XmlData.xml with these contents
<?xml version="1.0" encoding="utf-8"?>
<pm>
  <data>
    <item>
      <name ID="B">d0</name>
      <value>9.381083</value>
    </item>
    <item>
      <name ID="P">d1</name>
      <value>20.34651</value>
    </item>
    <item>
      <name ID="K">d2</name>
      <value>31.1635</value>
    </item>
    <item>
      <name ID="P">d3</name>
      <value>39.94775</value>
    </item>
    <item>
      <name ID="D">d4</name>
      <value>48.76433</value>
    </item>
  </data>
</pm>
Save the XML file, for example, to C:\XmlData.xml. Do not forget to save the file in Unicode UTF-8 format. For example in the Notepad program select "Save as" and change the coding to UTF-8.
 
Include the following scripts mentioned in examples into the PROMOTIC application. After the application is launched and the script processed the results will be displayed in Debug_info.
The common part of the script:
This is the common part of the script for all examples
Dim oXml, b
Set oXml = CreateObject("Microsoft.XMLDOM")    ' create ActiveX object
oXml.async = false    ' set synchronize load file
b = oXml.load("c:\XmlData.xml")    ' connect/load XML file
'Pm.Debug "ActiveX" & b     ' create ActiveX object
'Pm.Debug oXml, true    ' displaying the appropriate method
'Pm.Debug oXml.readyState   ' view connect state: 0=uninitialized, 1=loading, 2=loaded, 3=interactive, 4=complete
'MsgBox oXml.xml    ' view list page
1. Example:
The example reads the field of all tags immersed into the pm/data/item structure of the XML dokument. Then it loops through all tags <item> and pass into the INFO system, using the Debug method, all texts containing tags <name> and <value>
Dim arrNodes, i, lastNode
Set arrNodes = oXml.selectNodes("//pm/data/item")    ' array tags
lastNode = arrNodes.length - 1    ' total tag count
For i=0 To lastNode    ' write values array "arrNodes"
  Pm.Debug i & " tag name = " & arrNodes.Item(i).selectSingleNode("name").Text
  Pm.Debug i & " tag value = " & arrNodes.Item(i).selectSingleNode("value").Text
Next
 
it is also possible to use this syntax
 
Dim arrNodes, i, lastNode
Set arrNodes = getElementsByTagName("item")
lastNode = arrNodes.length - 1    
For i=0 To lastNode
  Pm.Debug i & " tag name = " & arrNodes.Item(i).childNodes(0).Text
  Pm.Debug i & " tag value = " & arrNodes.Item(i).childNodes(1).Text
Next
2. Example:
The example reads the field of all tags immersed into the pm/data/item structure of the XML dokument. Then it loops through all tags <name> and pass into the INFO system, using the Debug method, the name and value of the attribute.
Dim arrNodes, i, oAttr
Set arrNodes = oXml.selectNodes("//pm/data/item/name")
For i=0 To arrNodes.length - 1
  Set oAttr = arrNodes.Item(i).Attributes.Item(0)    ' the oAttr object contains all attributes of the "name" tag
  Pm.Debug oAttr.baseName     ' attribute name
  Pm.Debug oAttr.nodeValue     ' attribute value
Next
3. Example:
The example finds first occurrence of the <name> tag in the XML document, having the ID="P" attribute. The text value of the tag is displayed ssing the Debug method.
' value of the first "name" tag having the ID="P" attribute
Dim oNode
Set oNode = oXml.selectSingleNode("//pm/data/item/name[@ID='P']")
Pm.Debug oNode.Text
4. Example:
Uses the Debug method to report ID attribute value of each <name> tag in the XML document.
Dim Dim arrNodes, i
Set arrNodes = getElementsByTagName("name")
For i=0 To arrNodes.length - 1
  Pm.Debug arrNodes.Item(i).getAttribute("ID")
Next
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice