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 /* libxml2 ast_xml implementation. */
35 int ast_xml_init(void)
42 int ast_xml_finish(void)
49 struct ast_xml_doc *ast_xml_open(char *filename)
57 doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
59 return (struct ast_xml_doc *) doc;
63 void ast_xml_close(struct ast_xml_doc *doc)
69 xmlFreeDoc((xmlDoc *) doc);
74 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
82 root_node = xmlDocGetRootElement((xmlDoc *) doc);
84 return (struct ast_xml_node *) root_node;
87 void ast_xml_free_node(struct ast_xml_node *node)
93 xmlFreeNode((xmlNode *) node);
97 void ast_xml_free_attr(const char *attribute)
100 xmlFree((char *) attribute);
104 void ast_xml_free_text(const char *text)
107 xmlFree((char *) text);
111 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
123 attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
125 return (const char *) attrvalue;
128 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
130 struct ast_xml_node *cur;
137 for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
138 /* Check if the name matchs */
139 if (strcmp(ast_xml_node_get_name(cur), name)) {
142 /* We need to check for a specific attribute name? */
143 if (!attrname || !attrvalue) {
146 /* Get the attribute, we need to compare it. */
147 if ((attr = ast_xml_get_attribute(cur, attrname))) {
148 /* does attribute name/value matches? */
149 if (!strcmp(attr, attrvalue)) {
150 ast_xml_free_attr(attr);
153 ast_xml_free_attr(attr);
160 const char *ast_xml_get_text(struct ast_xml_node *node)
166 return (const char *) xmlNodeGetContent((xmlNode *) node);
169 const char *ast_xml_node_get_name(struct ast_xml_node *node)
171 return (const char *) ((xmlNode *) node)->name;
174 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
176 return (struct ast_xml_node *) ((xmlNode *) node)->children;
179 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
181 return (struct ast_xml_node *) ((xmlNode *) node)->next;
184 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
186 return (struct ast_xml_node *) ((xmlNode *) node)->prev;
189 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
191 return (struct ast_xml_node *) ((xmlNode *) node)->parent;
194 #endif /* defined(HAVE_LIBXML2) */