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.

          <employee type='PT'>
               <name>John Smith</name>
          <employee type='PT'>
               <name>Sue Jones</name>

Read information from an XML tree

The following functions read information from an XML tree:

Extracts XML string fragments from an XML string.
Performs a query on an XML string.
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:


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

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:

Deletes fragments from an XML string.
Inserts a new XML fragment into an XML string.
Replaces fragments of an XML string.
Sets the values of XML elements found by a query.
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");

          "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);

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

Back to top