Example: lr_xml_set_values

The following example uses lr_xml_set_values to change the phone extensions of two employee elements in an XML string.

First, a simple XML string is stored in parameter XML_Input_Param. Since we want two values to be matched and substituted, we prepare two new parameters, ExtensionParam_1 and ExtensionParam_2, and set them to two new phone extensions, 1111 and 2222.

lr_xml_set_values is invoked passing "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 indeed substituted.

For more examples of the use of lr_xml_set_values, see Set attribute values in Example Scripts for XML Queries.

#include "as_web.h"
char * xml_input = 
"<acme_org>"
    " <accounts_dept>"
        "<employee>"
            " <name>John Smith</name>"
            "<cubicle>227</cubicle>"
            "<extension>2145</extension>"
        "</employee>"
    "</accounts_dept>"
    "<engineering_dept>"
        "<employee>"
            "<name>Sue Jones</name>"
            "<extension>2375</extension>"
        "</employee>"
    "</engineering_dept>"
"</acme_org>";
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;
}

Example: Output:
Action.c(40): Retrieved value 1 : 1111
Action.c(40): Retrieved value 2 : 2222