//-----------------------------------------------------------------------------
// IXMLStorage.js file
//-----------------------------------------------------------------------------
// There is described the usage of the objects
// IXMLStorage and IXMLStrorageNode in the example.
//-----------------------------------------------------------------------------
//@ Start Example
var Storage = new ActiveXObject('TSObjectLibrary.XMLStorage');
var StorageNode = new ActiveXObject('TSObjectLibrary.XMLStorageNode');
// Storage object of the node tree representation in the text file
var Value = new ActiveXObject('TSObjectLibrary.Value');
Value.Value = new ActiveXObject('TSObjectLibrary.StringsList');
// The function of the recursive tree building
function BuildTree(Level, Item, Value) {
// The string to store the last result
var TotalString = new String();
// The string of the node position allignment in accordance to the width of the lower level
var WidthString = new String();
// Transfer to the lower level
Level++;
// Creating the current depth of the iteration for the text representation,
// under condition it has not been reached.
if (Level >= Value.Value.Count) {
Value.Value.Add('');
}
// Reading the level structure string by adding nodes
// the same level
TotalString = Value.Value.Items(Level);
// Alignment of the upper level node in accordance to the width of the lower nodes.
for (var k = Level + 1; k < Value.Value.Count; k++) {
WidthString = Value.Value.Items(k);
while (TotalString.length < WidthString.length) {
TotalString += '_';
}
}
// Adding the element to the text representation
TotalString = TotalString + '<' + Item.Name + '>';
Value.Value.Items(Level) = TotalString;
// Recursive call to build the tree for all the child nodes
for (var i = 0; i < Item.Count; i++) {
BuildTree(Level, Item.Items(i), Value);
}
// Transfer to the upper level in the tree after the iteration has been completed
// current node
Level--;
// Displaying the message that the node tree iteration has been completed
if (Level < 0) {
WScript.Echo("The node tree iteration has been completed");
}
}
// File for the XML loading (correct XML file is required)
// in the specified path)
var FileName = 'c:\XMLExample.xml';
// Loading the XML data from the file
Storage.LoadFromFile(FileName);
// Displaying the XML text
var MessageString = Storage.Text;
WScript.Echo(MessageString);
// Root node
StorageNode = Storage.RootNode;
// Building the tree for the loaded XML
BuildTree(-1, StorageNode, Value);
// Displaying the XML node tree
MessageString = Value.Value.Text;
WScript.Echo(MessageString);
//-----------------------------------------------------------------------------
// The IXMLStorageNode use
//-----------------------------------------------------------------------------
// Displaying the root node name
var Name = StorageNode.Name;
WScript.Echo(Name);
// Displaying the 'ID' parameter value of the root node under condition the 'ID' parameter exists
if (StorageNode.GetIsAttributeExists('ID')) {
WScript.Echo(StorageNode.GetAttributeAsStr('ID', "Default value"));
}
// Creating the child node 'Structure' (under condition it is not availabale) and
// Saving the text representation of the tree in the the given node attribute
var ChildNode = StorageNode.GetChildNode('Structure');
ChildNode.SetAttributeAsVariant('Text', Value.Value.Text);
// The Date/Time attributes use
// Creating a variable the Date/Time type use
var NodeDateTime = new Date().getVarDate();
// Setting the 'true' value to the flag of attributes recording
// The attributes will be saved to the XML,
// although, their values equal the default values
Storage.StoreDefaultValues = true;
// Creating the 'Time' attribute and setting its value
StorageNode.SetAttributeAsTime('Time', NodeDateTime, NodeDateTime);
// Creating the 'Date' attribute and setting its value
StorageNode.SetAttributeAsDate('Date', NodeDateTime, NodeDateTime);
// Creating the 'DateTime' attribute and setting its value
StorageNode.SetAttributeAsDateTime('DateTime', NodeDateTime, NodeDateTime);
// Creating the 'VariantDateTime' attribute and setting its value
StorageNode.SetAttributeAsVariant('VariantDateTime', NodeDateTime);
// The integer, boolean and string attributes use
// Creating variables of the required types
var BoolValue = true;
var StringValue = new String("String example");
var FloatValue = 5.9349;
// Creating the 'StringValue' attribute and setting the string value
StorageNode.SetAttributeAsStr('StringValue', StringValue, StringValue);
// Creating the 'BoolValue' attribute and setting the boolean value
StorageNode.SetAttributeAsBool('BoolValue', BoolValue, BoolValue);
// Creating the 'FloatValue' attribute and setting its value
// float type
StorageNode.SetAttributeAsFloat('FloatValue', FloatValue, FloatValue);
// Creating the child node 'Includes' (under condition it is not availabale).
ChildNode = StorageNode.GetChildNode('Includes');
// Creating the 'FloatValue' attribute of the 'Includes' node and setting
// float value
ChildNode.SetAttributeAsFloat('FloatValue', FloatValue, FloatValue);
// Reading the 'FloatValue' of the given attribute
WScript.Echo(ChildNode.GetAttributeAsVariant('FloatValue'));
// Reading the value of the attribute, which contains a string
// incorrect integer value
ChildNode.SetAttributeAsStr('IntStr', "Incorrect integer value","");
WScript.Echo(ChildNode.GetAttributeAsInt('IntStr', 25));
// Displaying the XML text that has been changed
WScript.Echo(Storage.Text);
// Saving to the XML file
FileName = 'c:\XMLExampleOutput.xml';
Storage.SaveToFile(FileName, 'UTF-8');
// Displaying the message about the file, where changes have been saved
WScript.Echo("XML data changes can be viewed in the file " + FileName);
//@ End Example
|
Copyright (с) Terrasoft 2002-2007.
|