These examples describe how to
read specific values or parameters into the PROMOTIC application from the 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 created first in the variable of the
String type) or
Pm.FileTextReplace (the template of the XML file is only 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 encoding to
UTF-8.
Include the following scripts mentioned in examples into the PROMOTIC application.
After the application is launched 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, aNodes, iItm, 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 methods
// Pm.Debug(oXml.readyState); // Displays connection state: 0=uninitialized, 1=loading, 2=loaded, 3=interactive, 4=complete
// Pm.MessageBox("xml text", oXml.xml, 0); // Displays page content
Dim oXml, b, aNodes, iItm, 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 methods
' Pm.Debug oXml.readyState ' Displays connection 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
var iItm;
aNodes = oXml.selectNodes("//pm/data/item");
// The array of tags
lastNode = aNodes.length - 1;
// The total number of tags
// Writing all values array "aNodes"
for (iItm = 0; iItm <= lastNode; iItm++)
{
Pm.Debug(iItm + " TagName=" + aNodes.item(iItm).selectSingleNode("name").text);
Pm.Debug(iItm + " TagValue=" + aNodes.item(iItm).selectSingleNode("value").text);
}
Dim iItm
Set aNodes = oXml.selectNodes("//pm/data/item")
' The array of tags
lastNode = aNodes.length - 1
' The total number of tags
' Writing all values array "aNodes"
For iItm = 0 To lastNode
Pm.Debug(iItm & " TagName=" & aNodes.item(iItm).selectSingleNode("name").text)
Pm.Debug iItm & " TagValue=" & aNodes.item(iItm).selectSingleNode("value").text
Next
It is also can be used this syntax:
JavaScriptVBScriptSelect and copy to clipboard
var iItm;
aNodes = oXml.getElementsByTagName("item");
// The array of tags
lastNode = aNodes.length - 1;
// The total number of tags
// Writing all values array "aNodes"
for (iItm = 0; iItm <= lastNode; iItm++)
{
Pm.Debug(iItm + " TagName=" + aNodes.item(iItm).childNodes(0).text);
Pm.Debug(iItm + " TagValue=" + aNodes.item(iItm).childNodes(1).text);
}
Dim iItm
Set aNodes = oXml.getElementsByTagName("item")
' The array of tags
lastNode = aNodes.length - 1
' The total number of tags
' Writing all values array "aNodes"
For iItm = 0 To lastNode
Pm.Debug(iItm & " TagName=" & aNodes.item(iItm).childNodes(0).text)
Pm.Debug iItm & " TagValue=" & aNodes.item(iItm).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
var iItm;
aNodes = oXml.selectNodes("//pm/data/item/name");
// The array of tags
lastNode = aNodes.length - 1;
// The total number of tags
// Writing all values array "aNodes"
for (iItm = 0; iItm <= lastNode; iItm++)
{
oAttr = aNodes.item(iItm).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
}
Dim iItm
Set aNodes = oXml.selectNodes("//pm/data/item/name")
' The array of tags
lastNode = aNodes.length - 1
' The total number of tags
' Writing all values array "aNodes"
For iItm = 0 To lastNode
Set oAttr = aNodes.item(iItm).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
aNodes = oXml.getElementsByTagName("name");
lastNode = aNodes.length - 1;
for (iItm = 0; iItm <= lastNode; iItm++)
{
Pm.Debug(aNodes.item(iItm).getAttribute("ID"));
}
Set aNodes = oXml.getElementsByTagName("name")
lastNode = aNodes.length - 1
For iItm = 0 To lastNode
Pm.Debug aNodes.item(iItm).getAttribute("ID")
Next