2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief XML abstraction layer
21 * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
25 #include "asterisk/xml.h"
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29 #if defined(HAVE_LIBXML2)
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xinclude.h>
33 /* libxml2 ast_xml implementation. */
36 int ast_xml_init(void)
43 int ast_xml_finish(void)
50 struct ast_xml_doc *ast_xml_open(char *filename)
58 doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
60 /* process xinclude elements. */
61 if (xmlXIncludeProcess(doc) < 0) {
67 return (struct ast_xml_doc *) doc;
70 void ast_xml_close(struct ast_xml_doc *doc)
76 xmlFreeDoc((xmlDoc *) doc);
81 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
89 root_node = xmlDocGetRootElement((xmlDoc *) doc);
91 return (struct ast_xml_node *) root_node;
94 void ast_xml_free_node(struct ast_xml_node *node)
100 xmlFreeNode((xmlNode *) node);
104 void ast_xml_free_attr(const char *attribute)
107 xmlFree((char *) attribute);
111 void ast_xml_free_text(const char *text)
114 xmlFree((char *) text);
118 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
130 attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
132 return (const char *) attrvalue;
135 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
137 struct ast_xml_node *cur;
144 for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
145 /* Check if the name matchs */
146 if (strcmp(ast_xml_node_get_name(cur), name)) {
149 /* We need to check for a specific attribute name? */
150 if (!attrname || !attrvalue) {
153 /* Get the attribute, we need to compare it. */
154 if ((attr = ast_xml_get_attribute(cur, attrname))) {
155 /* does attribute name/value matches? */
156 if (!strcmp(attr, attrvalue)) {
157 ast_xml_free_attr(attr);
160 ast_xml_free_attr(attr);
167 const char *ast_xml_get_text(struct ast_xml_node *node)
173 return (const char *) xmlNodeGetContent((xmlNode *) node);
176 const char *ast_xml_node_get_name(struct ast_xml_node *node)
178 return (const char *) ((xmlNode *) node)->name;
181 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
183 return (struct ast_xml_node *) ((xmlNode *) node)->children;
186 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
188 return (struct ast_xml_node *) ((xmlNode *) node)->next;
191 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
193 return (struct ast_xml_node *) ((xmlNode *) node)->prev;
196 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
198 return (struct ast_xml_node *) ((xmlNode *) node)->parent;
201 #endif /* defined(HAVE_LIBXML2) */