Example: lr_xml_extract
The following example uses lr_xml_extract to retrieve the employees in the engineering department from the XML string xml_input. lr_xml_extract extracts the entire string fragment that makes up the tag /acme_org/engineering_dept/employee.
The resulting fragment is stored in the XMLFragmentParam parameter Result, which is sent to the output at the end of the test.
For more examples of the use of lr_xml_extract, see Get a fragment 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 level=\"manager\">"
"<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_xml_extract("XML={XML_Input_Param}",
"XMLFragmentParam=Result",
"Query=/acme_org/engineering_dept/employee", LAST );
lr_output_message(lr_eval_string("Extracted: {Result}"));
return 0;
}
Example: Output:
Action.c(31): Extracted: <employee level="manager"><name>Sue Jones</name><extension>2375</extension></employee>
Example 2
In the following example, lr_xml_extract extracts an attribute and its value from the employee element.
lr_xml_extract( "Xml={XML_Input_Param}",
"Query=/acme_org/engineering_dept/employee/@level",
"XMLFragmentParam=Result",
LAST
);
lr_output_message(lr_eval_string("***** {Result} *****"));
Example: Output:
Action1.c(117): "lr_xml_extract" was successful, 1 match processed
Action1.c(122): ***** level="manager" *****