Storages.XML Method

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Use the XML method to create or load data from a .xml file that was created earlier using the XML method. The method returns the FileSection object that specifies the file’s root section.

To create an empty XML file, use the XML method with an empty parameter value. Later, you can save the created file on the disk by calling the FileSection.Save or FileSection.SaveAs method.

Declaration

Storages.XML(FileName)

FileName [in]    Required    String    
Result A FileSection object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

FileName

Specifies the full path to the existing .xml file that was created earlier using the XML method.

If you want to create an empty XML file using the XML method, specify an empty parameter for the method. In other words, to create a file, use the following code: Storages.XML("") in VBScript, JavaScript, JScript, Python, C++Script and C#Script, and Storages.XML('') in DephiScript.

Result Value

A FileSection object.

Remarks

XML can only open those files that were created by this method (they have a specific structure). If the file was not created by XML, an exception occurs.

For more information on the structure of the XML files created via this method, see Section Object.

Example

The following example demonstrates how to modify values stored in an existing XML file.

JavaScript, JScript

function Test()
{

  var FileSection, FilePath, SectionName, OptionName, Section, NewValue;

  // Specifies the path to the XML file
  FilePath = "D:\\Work Folder\\XMLFile.xml";
  // Loads data from the XML file
  FileSection = Storages.XML(FilePath);
  // Specifies the name of the section that contains the needed option

  SectionName = "Section1";
  // Specifies the name of the option whose value we want to change
  OptionName = "Option1";
  // Specifies a new value for the option
  NewValue = "New Value";

  // Obtains the section by its name
  Section = FileSection.GetSubSection(SectionName);

  // Checks if the option belongs to the obtained section
  if (Section.OptionExists(OptionName))
    {
    // Sets a new value for the option
    Section.SetOption(OptionName, NewValue);
    Log.Message("The " + OptionName + " option value has been modified");
    // Saves the changes
    FileSection.Save();
    }
  else
    Log.Warning("The " + OptionName + " option does not exist");

}

Python

def Test():

  # Specifies the path to the XML file
  FilePath = "D:\\Work Folder\\XMLFile.xml"
  # Loads data from the XML file
  FileSection = Storages.XML(FilePath)
  # Specifies the name of the section that contains the needed option

  SectionName = "Section1"
  # Specifies the name of the option whose value we want to change
  OptionName = "Option1"
  # Specifies a new value for the option
  NewValue = "New Value"

  # Obtains the section by its name
  Section = FileSection.GetSubSection(SectionName)

  # Checks if the option belongs to the obtained section
  if (Section.OptionExists(OptionName)):
    # Sets a new value for the option
    Section.SetOption(OptionName, NewValue)
    Log.Message("The " + OptionName + " option value has been modified")
    # Saves the changes
    FileSection.Save()
  else:
    Log.Warning("The " + OptionName + " option does not exist")

VBScript

Sub Test

  Dim FileSection, FilePath, SectionName, OptionName, Section, NewValue

  ' Specifies the path to the XML file
  FilePath = "D:\Work Folder\XMLFile.xml"
  ' Loads data from the XML file
  Set FileSection = Storages.XML(FilePath)
  ' Specifies the name of the section that contains the needed option

  SectionName = "Section1"
  ' Specifies the name of the option whose value we want to change
  OptionName = "Option1"
  ' Specifies a new value for the option
  NewValue = "New Value"

  ' Obtains the section by its name
  Set Section = FileSection.GetSubSection(SectionName)

  ' Checks if the option belongs to the obtained section
  If Section.OptionExists(OptionName) Then
    ' Sets a new value for the option
    Call Section.SetOption(OptionName, NewValue)
    Log.Message("The " & OptionName & " option value has been modified")
    ' Saves the changes
    FileSection.Save
  Else
    Log.Warning("The " & OptionName & " option does not exist")
  End If

End Sub

DelphiScript

procedure Test();
var FileSection, FilePath, SectionName, OptionName, Section, NewValue;

begin

  // Specifies the path to the XML file
  FilePath := 'D:\Work Folder\XMLFile.xml';
  // Loads data from the XML file
  FileSection := Storages.XML(FilePath);
  // Specifies the name of the section that contains the needed option

  SectionName := 'Section1';
  // Specifies the name of the option whose value we want to change
  OptionName := 'Option1';
  // Specifies a new value for the option
  NewValue := 'New Value';

  // Obtains the section by its name
  Section := FileSection.GetSubSection(SectionName);

  // Checks if the option belongs to the obtained section
  if Section.OptionExists(OptionName) then
    begin
    // Sets a new value for the option
    Section.SetOption(OptionName, NewValue);
    Log.Message('The ' + OptionName + ' option value has been modified');
    // Saves the changes
    FileSection.Save();
    end
  else
    Log.Warning('The ' + OptionName + ' option does not exist');

end;

C++Script, C#Script

function Test()
{

  var FileSection, FilePath, SectionName, OptionName, Section, NewValue;

  // Specifies the path to the XML file
  FilePath = "D:\\Work Folder\\XMLFile.xml";
  // Loads data from the XML file
  FileSection = Storages["XML"](FilePath);
  // Specifies the name of the section that contains the needed option

  SectionName = "Section1";
  // Specifies the name of the option whose value we want to change
  OptionName = "Option1";
  // Specifies a new value for the option
  NewValue = "New Value";

  // Obtains the section by its name
  Section = FileSection["GetSubSection"](SectionName);

  // Checks if the option belongs to the obtained section
  if (Section["OptionExists"](OptionName))
    {
    // Sets a new value for the option
    Section["SetOption"](OptionName, NewValue);
    Log["Message"]("The " + OptionName + " option value has been modified");
    // Saves the changes
    FileSection["Save"]();
    }
  else
    Log["Warning"]("The " + OptionName + " option does not exist");

}

For an example on how to create XML files using the XML method, see Working With XML Files From Scripts.

See Also

FileSection Object
Binary Method
INI Method
Registry Method
Working With XML Files From Scripts

Highlight search results