Share this page

# Use XML functions

This section provides examples of how to work with data in an XML tree. Certain functions allow you to retrieve information, and others let you write information to an XML tree. The examples use the following XML tree containing the names and extensions of several employees in the Acme organization.

    <acme_org>
         <accounting_dept>
              <employee type='PT'>
                   <name>John Smith</name>
                   <extension>2145</extension>
              </employee>
         </accounting_dept>
         <engineering_dept>
              <employee type='PT'>
                   <name>Sue Jones</name>
                   <extension>2375</extension>
              </employee>
         </engineering_dept>
    </acme_org>

In this topic:

## Read information from an XML tree

The following functions read information from an XML tree:

Function | Description
--- | ---
lr_xml_extract | Extracts XML string fragments from an XML string.
lr_xml_find | Performs a query on an XML string.
lr_xml_get_values | Retrieves values of XML elements found by a query.

To retrieve a specific value through a query, you specify the tags of the parent and child nodes in a path format. For information about how to retrieve multiple values, see Multiple query matching

For example, to retrieve an employee name in the Accounting department, use the following string:

    lr_xml_get_values("XML={XML_Input_Param}",
    "ValueParam=OutputParam",
    "Query=/acme_org/accounting_dept/employee/name",
    LAST);

The Execution log window (with Extended logging enabled) shows the output of this function:

Output:
Action.c(20): "lr_xml_get_values" was successful, 1 match processed
Action.c(25): Query result = John Smith

Back to top

## Write to an XML tree

The following functions write values to an XML tree:

lr_xml_delete | Deletes fragments from an XML string.
lr_xml_insert | Inserts a new XML fragment into an XML string.
lr_xml_replace | Replaces fragments of an XML string.
lr_xml_set_values | Sets the values of XML elements found by a query.
lr_xml_transform | Applies Extensible Stylesheet Language (XSL) transformation to XML data.

The most common writing function is lr_xml_set_values which sets the values of specified elements in an XML string. The following example uses lr_xml_set_values to change the phone extensions of two employee elements in an XML string.

First, save the XML string to a parameter called XML_Input_Param. The two values should be matched and substituted, so you prepare two new parameters, ExtensionParam_1 and ExtensionParam_2, and set their values to two new phone extensions, 1111 and 2222.

lr_xml_set_values contains the argument "ValueName=ExtensionParam", which picks up the values of ExtensionParam_1 and ExtensionParam_2. The current extensions of the two employees are substituted with the values of these parameters, 1111 and 2222. The value of OutputParam is then evaluated proving that the new phone extensions were in fact substituted.

    Action() {

         int i, NumOfValues;
         char buf[64];

         lr_save_string(xml_input, "XML_Input_Param"); // Save input as parameter
         lr_save_string("1111", "ExtensionParam_1");
         lr_save_string("2222", "ExtensionParam_2");

         lr_xml_set_values("XML={XML_Input_Param}",
              "ResultParam=NewXmlParam", "ValueParam=ExtensionParam",
              "SelectAll=yes", "Query=//extension", LAST);

         NumOfValues= lr_xml_get_values("XML={NewXmlParam}",
              "ValueParam=OutputParam", "Query=//extension",
              "SelectAll=yes", LAST);

         for (i = 0; i < NumOfValues; i++) {/* Print the multiple values of MultiParam */

              sprintf(buf, "Retrieved value %d : {OutputParam_%d}", i+1, i+1);
              lr_output_message(lr_eval_string(buf));
         }

         return 0;
    }
    Output:
    Action.c(40): Retrieved value 1: 1111
    Action.c(40): Retrieved value 2: 2222

Back to top

https://admhelp.microfocus.com VuGen,Professional Performance Engineering,Performance Engineering for Developers 8942
Tips to improve your search results... OpenText™ Virtual User Generator Help Center OpenText™ VuGen Help 25.1-25.3 All View Local Help Center View Online Help Center Version: