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 #ifndef _POSIX_C_SOURCE /* Needed on Mac OS X */
31 #define _POSIX_C_SOURCE 200112L
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xinclude.h>
36 /* libxml2 ast_xml implementation. */
39 int ast_xml_init(void)
46 int ast_xml_finish(void)
53 struct ast_xml_doc *ast_xml_open(char *filename)
61 doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
63 /* process xinclude elements. */
64 if (xmlXIncludeProcess(doc) < 0) {
70 return (struct ast_xml_doc *) doc;
73 void ast_xml_close(struct ast_xml_doc *doc)
79 xmlFreeDoc((xmlDoc *) doc);
84 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
92 root_node = xmlDocGetRootElement((xmlDoc *) doc);
94 return (struct ast_xml_node *) root_node;
97 void ast_xml_free_node(struct ast_xml_node *node)
103 xmlFreeNode((xmlNode *) node);
107 void ast_xml_free_attr(const char *attribute)
110 xmlFree((char *) attribute);
114 void ast_xml_free_text(const char *text)
117 xmlFree((char *) text);
121 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
133 attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
135 return (const char *) attrvalue;
138 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
140 struct ast_xml_node *cur;
147 for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
148 /* Check if the name matchs */
149 if (strcmp(ast_xml_node_get_name(cur), name)) {
152 /* We need to check for a specific attribute name? */
153 if (!attrname || !attrvalue) {
156 /* Get the attribute, we need to compare it. */
157 if ((attr = ast_xml_get_attribute(cur, attrname))) {
158 /* does attribute name/value matches? */
159 if (!strcmp(attr, attrvalue)) {
160 ast_xml_free_attr(attr);
163 ast_xml_free_attr(attr);
170 const char *ast_xml_get_text(struct ast_xml_node *node)
176 return (const char *) xmlNodeGetContent((xmlNode *) node);
179 const char *ast_xml_node_get_name(struct ast_xml_node *node)
181 return (const char *) ((xmlNode *) node)->name;
184 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
186 return (struct ast_xml_node *) ((xmlNode *) node)->children;
189 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
191 return (struct ast_xml_node *) ((xmlNode *) node)->next;
194 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
196 return (struct ast_xml_node *) ((xmlNode *) node)->prev;
199 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
201 return (struct ast_xml_node *) ((xmlNode *) node)->parent;
204 #endif /* defined(HAVE_LIBXML2) */