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 of 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 the 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 result will be displayed in the
Debug item of the INFO system.
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, 0); // Displays page content
Dim oXml, b, arrNodes, i, lastNode, oAttr
Set oXml = CreateObject("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, &H0 ' 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 of tags
lastNode = arrNodes.length -1;
// total tag count
// write values array "arrNodes"
for (i = 0; i <= lastNode; i++)
{
Pm.Debug(i + " TagName=" + arrNodes.item(i).selectSingleNode("name").text);
Pm.Debug(i + " TagValue=" + arrNodes.item(i).selectSingleNode("value").text);
}
Set arrNodes = oXml.selectNodes("//pm/data/item")
' array of tags
lastNode = arrNodes.length -1
' total tag count
' write values array "arrNodes"
For i = 0 To lastNode
Pm.Debug i & " TagName=" & arrNodes.item(i).selectSingleNode("name").text
Pm.Debug i & " TagValue=" & arrNodes.item(i).selectSingleNode("value").text
Next
It is also can be used this syntax:
JavaScriptVBScriptSelect and copy to clipboard
arrNodes = oXml.getElementsByTagName("item");
// array of tags
lastNode = arrNodes.length -1;
// total tag count
// write values array "arrNodes"
for (i = 0; i <= lastNode; i++)
{
Pm.Debug(i + " TagName=" + arrNodes.item(i).childNodes(0).text);
Pm.Debug(i + " TagValue=" + arrNodes.item(i).childNodes(1).text);
}
Set arrNodes = oXml.getElementsByTagName("item")
' array of tags
lastNode = arrNodes.length -1
' total tag count
' write values array "arrNodes"
For i = 0 To lastNode
Pm.Debug i & " TagName=" & arrNodes.item(i).childNodes(0).text
Pm.Debug i & " TagValue=" & arrNodes.item(i).childNodes(1).text
Next
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 of 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
}
Set arrNodes = oXml.selectNodes("//pm/data/item/name")
' array of tags
lastNode = arrNodes.length -1
' total tag count
' write values array "arrNodes"
For i = 0 To lastNode
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
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);
' 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
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"));
}
Set arrNodes = oXml.getElementsByTagName("name")
lastNode = arrNodes.length -1
For i = 0 To lastNode
Pm.Debug arrNodes.item(i).getAttribute("ID")
Next