Include file for XML utilities library. More...
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <misc.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
Go to the source code of this file.
Data Structures | |
struct | xmlConfig_t |
XML structure to deal with DOM using XPath. More... | |
Defines | |
#define | _GNU_SOURCE |
GNU extensions. | |
#define | TRUE 1 |
TRUE value macro is 1. | |
#define | FALSE 0 |
FALSE value macro is 0. | |
Functions | |
void | xml_free_config (xmlConfig_t *conf) |
Free memory for an xmlConfig_t structure. | |
xmlConfig_t * | xml_load_config (char *filename) |
Read an XML file into memory. | |
xmlNodePtr | xml_get_node_by_xpath (xmlConfig_t *conf, char *path) |
Get XML setting element node. | |
xmlChar * | xml_get_setting (xmlConfig_t *conf, char *path) |
Get XML setting element value. | |
void | xml_print_setting (xmlConfig_t *conf, char *path) |
Show XML setting element value. |
Include file for XML utilities library.
Definition in file xml_utils.h.
#define _GNU_SOURCE |
GNU extensions.
Definition at line 55 of file xml_utils.h.
#define FALSE 0 |
FALSE value macro is 0.
Definition at line 91 of file xml_utils.h.
#define TRUE 1 |
TRUE value macro is 1.
Definition at line 89 of file xml_utils.h.
void xml_free_config | ( | xmlConfig_t * | conf | ) |
Free memory for an xmlConfig_t structure.
[in] | conf | XML information for DOM and XPath |
Definition at line 58 of file xml_free_config.c.
References xmlConfig_t::ctxt, xmlConfig_t::doc, and xmlConfig_t::file.
Referenced by load_conf(), and xml_load_config().
xmlNodePtr xml_get_node_by_xpath | ( | xmlConfig_t * | conf, | |
char * | path | |||
) |
Get XML setting element node.
[in] | conf | XML information for DOM and XPath |
[in] | path | path for XPath |
Definition at line 58 of file xml_get_node_by_xpath.c.
References xmlConfig_t::ctxt.
Referenced by xml_get_setting().
00058 { 00066 xmlXPathObjectPtr xpathRes; 00067 xmlNodePtr nodeptr = NULL; 00068 00069 /* Evaluate XPath expression */ 00070 xpathRes = xmlXPathEvalExpression((unsigned char *) path, conf->ctxt); 00071 00072 if (xpathRes) 00073 if (xpathRes->nodesetval) { 00074 00075 if (xpathRes->type == XPATH_NODESET && xpathRes->nodesetval->nodeNr == 1) 00076 nodeptr = xpathRes->nodesetval->nodeTab[0]; 00077 (void) xmlXPathFreeObject(xpathRes); 00078 00079 } 00080 00081 return nodeptr; 00082 }
xmlChar* xml_get_setting | ( | xmlConfig_t * | conf, | |
char * | path | |||
) |
Get XML setting element value.
[in] | conf | XML information for DOM and XPath |
[in] | path | path for XPath |
Definition at line 58 of file xml_get_setting.c.
References xml_get_node_by_xpath().
Referenced by load_conf(), and xml_print_setting().
00058 { 00066 xmlNodePtr nodeptr; 00067 00068 /* Get DOM node pointer using XPath */ 00069 nodeptr = xml_get_node_by_xpath(conf, path); 00070 00071 if (nodeptr != NULL) { 00072 return xmlNodeGetContent(nodeptr); 00073 } 00074 else 00075 return NULL; 00076 }
xmlConfig_t* xml_load_config | ( | char * | filename | ) |
Read an XML file into memory.
[in] | filename | Input XML filename |
Definition at line 58 of file xml_load_config.c.
References alloc_error(), xmlConfig_t::ctxt, xmlConfig_t::doc, xmlConfig_t::file, xmlConfig_t::root, and xml_free_config().
Referenced by load_conf().
00058 { 00065 xmlConfig_t *conf = NULL; 00066 00067 conf = (void *) malloc(sizeof(xmlConfig_t)); 00068 if (conf == NULL) alloc_error(__FILE__, __LINE__); 00069 00070 /* Copy filename */ 00071 conf->file = strdup(filename); 00072 if (conf->file == NULL) alloc_error(__FILE__, __LINE__); 00073 00074 (void) xmlInitParser(); 00075 00076 /* Create DOM tree from XML file */ 00077 (void) xmlKeepBlanksDefault(0); 00078 conf->doc = xmlParseFile(conf->file); 00079 if (conf->doc == NULL) { 00080 (void) xml_free_config(conf); 00081 (void) xmlCleanupParser(); 00082 return NULL; 00083 } 00084 00085 /* Get root */ 00086 conf->root = xmlDocGetRootElement(conf->doc); 00087 if (conf->root != NULL && xmlStrcasecmp(conf->root->name, (unsigned const char *) "configuration")) { 00088 (void) xml_free_config(conf); 00089 (void) xmlCleanupParser(); 00090 return NULL; 00091 } 00092 00093 /* Initialize XPath environment */ 00094 (void) xmlXPathInit(); 00095 00096 /* Create XPath Context */ 00097 conf->ctxt = xmlXPathNewContext(conf->doc); 00098 if (conf->ctxt == NULL) { 00099 (void) xml_free_config(conf); 00100 (void) xmlCleanupParser(); 00101 return NULL; 00102 } 00103 00104 return conf; 00105 }
void xml_print_setting | ( | xmlConfig_t * | conf, | |
char * | path | |||
) |
Show XML setting element value.
[in] | conf | XML information for DOM and XPath |
[in] | path | path for XPath |
Definition at line 58 of file xml_print_setting.c.
References xml_get_setting().
00058 { 00064 xmlChar *val; /* Value */ 00065 00066 /* Get value using XPath */ 00067 val = xml_get_setting(conf, path); 00068 00069 (void) printf("Value of '%s' : %s\n", path, (char *) val != NULL ? (char *) val : "(setting unknown)"); 00070 }