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 by 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 file by 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 starting the application and the script processing 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
JavaScriptVBScriptSelect and copy to clipboard

var oXml, b, arrNodes, i, lastNode, oAttr;

oXml = Pm.AxGetObject("new", "Microsoft.XMLDOM");
oXml.async = false;  //set synchronize load file
b = oXml.load("C:\\XmlData.xml");  //connect/load XML file
//Pm.Debug("XMLDOM = " + b); // create ActiveX object
//Pm.Debug(oXml, true); // displays the appropriate method
//Pm.Debug(oXml.readyState); // displays connect state: 0=uninitialized, 1=loading, 2=loaded, 3=interactive, 4=complete
//Pm.MessageBox("xml text", oXml.xml, 0x0); // displays page content
1st 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, by the Debug method, all texts containing tags <name> and <value>
JavaScriptVBScriptSelect and copy to clipboard

arrNodes = oXml.selectNodes("//pm/data/item");  //array tags
lastNode = arrNodes.length -1;  //total tag count

//write values array "arrNodes"
for (i = 0; i <= lastNode; i++)
{
Pm.Debug(i + " tag name = " + arrNodes.item(i).selectSingleNode("name").text);
Pm.Debug(i + " tag value = " + arrNodes.item(i).selectSingleNode("value").text);
}
 
it is also can be used this syntax
 
JavaScriptVBScriptSelect and copy to clipboard

arrNodes = oXml.getElementsByTagName("item");  //array tags
lastNode = arrNodes.length -1;  //total tag count

//write values array "arrNodes"
for (i = 0; i <= lastNode; i++)
{
Pm.Debug(i + " tag name = " + arrNodes.item(i).childNodes(0).text);
Pm.Debug(i + " tag value = " + arrNodes.item(i).childNodes(1).text);
}
2nd 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, by the Debug method, the name and value of the attribute.
JavaScriptVBScriptSelect and copy to clipboard

arrNodes = oXml.selectNodes("//pm/data/item/name");  //array tags
lastNode = arrNodes.length -1;  //total tag count

//write values array "arrNodes"
for (i = 0; i <= lastNode; i++)
{
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
}
3rd Example:
The example finds first occurrence of the <name> tag in the XML document, having the ID="P" attribute and the text value of the tag is displayed by the Debug method.
JavaScriptVBScriptSelect and copy to clipboard

//value of the first "name" tag having the ID="P" attribute
var oNode = oXml.selectSingleNode("//pm/data/item/name[@ID='P']");
Pm.Debug(oNode.text);
4th Example:
To report ID attribute value of each <name> tag in the XML document.
JavaScriptVBScriptSelect and copy to clipboard

arrNodes = oXml.getElementsByTagName("name");
lastNode = arrNodes.length -1;

for (i = 0; i <= lastNode; i++)
Pm.Debug(arrNodes.item(i).getAttribute("ID"));

History:
Pm7.01.01: Created
PROMOTIC 9.0.4 SCADA system documentation - MICROSYS, spol. s r.o.

Send page remarkContact responsible person
Navigation:
 
- XML
 
- How to read common XML files in the application
 
 
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice