Asterisk - The Open Source Telephony Project  18.5.0
xml.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Eliel C. Sardanons (LU1ALY) <[email protected]>
5  *
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.
11  *
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.
15  */
16 
17 #ifndef _ASTERISK_XML_H
18 #define _ASTERISK_XML_H
19 
20 /*! \file
21  * \brief Asterisk XML abstraction layer
22  */
23 
24 struct ast_xml_node;
25 struct ast_xml_doc;
26 struct ast_xml_xpath_results;
27 
28 /*!
29  * \brief Initialize the XML library implementation.
30  * This function is used to setup everything needed
31  * to start working with the xml implementation.
32  * \retval 0 On success.
33  * \retval 1 On error.
34  */
35 int ast_xml_init(void);
36 
37 /*!
38  * \brief Cleanup library allocated global data.
39  * \retval 0 On success.
40  * \retval 1 On error.
41  */
42 int ast_xml_finish(void);
43 
44 /*!
45  * \brief Open an XML document.
46  * \param filename Document path.
47  * \retval NULL on error.
48  * \retval The ast_xml_doc reference to the open document.
49  */
50 struct ast_xml_doc *ast_xml_open(char *filename);
51 
52 /*!
53  * \brief Create a XML document.
54  * \retval NULL on error.
55  * \retval non-NULL The allocated document structure.
56  */
57 struct ast_xml_doc *ast_xml_new(void);
58 
59 /*!
60  * \brief Create a XML node.
61  * \param name The name of the node to be created.
62  * \retval NULL on error.
63  * \retval non-NULL The allocated node structe.
64  */
65 struct ast_xml_node *ast_xml_new_node(const char *name);
66 
67 /*!
68  * \brief Add a child node inside a passed parent node.
69  * \param parent The pointer of the parent node.
70  * \param child_name The name of the child node to add.
71  * \retval NULL on error.
72  * \retval non-NULL The created child node pointer.
73  */
74 struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name);
75 
76 /*!
77  * \brief Add a child node, to a specified parent node.
78  * \param parent Where to add the child node.
79  * \param child The child node to add.
80  * \retval NULL on error.
81  * \retval non-NULL The add child node on success.
82  */
83 struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child);
84 
85 /*!
86  * \brief Add a list of child nodes, to a specified parent node.
87  * \param parent Where to add the child node.
88  * \param child The child list to add.
89  * \retval NULL on error.
90  * \retval non-NULL The added child list on success.
91  */
92 struct ast_xml_node *ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child);
93 
94 /*!
95  * \brief Create a copy of a n ode list.
96  * \param list The list to copy.
97  * \retval NULL on error.
98  * \retval non-NULL The copied list.
99  */
100 struct ast_xml_node *ast_xml_copy_node_list(struct ast_xml_node *list);
101 
102 /*!
103  * \brief Close an already open document and free the used
104  * structure.
105  * \retval doc The document reference.
106  */
107 void ast_xml_close(struct ast_xml_doc *doc);
108 
109 /*! \brief Open an XML document that resides in memory.
110  * \param buffer The address where the document is stored
111  * \param size The number of bytes in the document
112  * \retval NULL on error.
113  * \retval The ast_xml_doc reference to the open document.
114  */
115 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size);
116 
117 /*!
118  * \brief Specify the root node of a XML document.
119  * \param doc The document pointer.
120  * \param node A pointer to the node we want to set as root node.
121  */
122 void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node);
123 
124 /*!
125  * \brief Get the document root node.
126  * \param doc Document reference
127  * \retval NULL on error
128  * \retval The root node on success.
129  */
130 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc);
131 
132 /*!
133  * \brief Free node
134  * \param node Node to be released.
135  */
136 void ast_xml_free_node(struct ast_xml_node *node);
137 
138 /*!
139  * \brief Free an attribute returned by ast_xml_get_attribute()
140  * \param attribute pointer to be freed.
141  */
142 void ast_xml_free_attr(const char *attribute);
143 
144 /*!
145  * \brief Get the document based on a node.
146  * \param node A node that is part of the dom.
147  * \returns The dom pointer where this node resides.
148  */
149 struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node);
150 
151 /*!
152  * \brief Free a content element that was returned by ast_xml_get_text()
153  * \param text text to be freed.
154  */
155 void ast_xml_free_text(const char *text);
156 
157 /*!
158  * \brief Get a node attribute by name
159  * \param node Node where to search the attribute.
160  * \param attrname Attribute name.
161  * \retval NULL on error
162  * \retval The attribute value on success.
163  */
164 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname);
165 
166 /*!
167  * \brief Set an attribute to a node.
168  * \param node In which node we want to insert the attribute.
169  * \param name The attribute name.
170  * \param value The attribute value.
171  * \retval 0 on success.
172  * \retval -1 on error.
173  */
174 int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value);
175 
176 /*!
177  * \brief Find a node element by name.
178  * \param root_node This is the node starting point.
179  * \param name Node name to find.
180  * \param attrname attribute name to match (if NULL it won't be matched).
181  * \param attrvalue attribute value to match (if NULL it won't be matched).
182  * \retval NULL if not found.
183  * \retval The node on success.
184  */
185 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue);
186 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name);
187 const char *ast_xml_get_ns_href(struct ast_xml_ns *ns);
188 
189 /*!
190  * \brief Get an element content string.
191  * \param node Node from where to get the string.
192  * \retval NULL on error.
193  * \retval The text content of node.
194  */
195 const char *ast_xml_get_text(struct ast_xml_node *node);
196 
197 /*!
198  * \brief Set an element content string.
199  * \param node Node from where to set the content string.
200  * \param content The text to insert in the node.
201  */
202 void ast_xml_set_text(struct ast_xml_node *node, const char *content);
203 
204 /*!
205  * \brief Get the name of a node. */
206 const char *ast_xml_node_get_name(struct ast_xml_node *node);
207 
208 /*!
209  * \brief Get the node's children. */
210 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node);
211 
212 /*!
213  * \brief Get the next node in the same level. */
214 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node);
215 
216 /*!
217  * \brief Get the previous node in the same leve. */
218 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node);
219 
220 /*!
221  * \brief Get the parent of a specified node. */
222 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node);
223 
224 /*!
225  * \brief Dump the specified document to a file. */
226 int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc);
227 
228 /*!
229  * \brief Free the XPath results
230  * \param results The XPath results object to dispose of
231  *
232  * \since 12
233  */
234 void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results);
235 
236 /*!
237  * \brief Return the number of results from an XPath query
238  * \param results The XPath results object to count
239  * \retval The number of results in the XPath object
240  *
241  * \since 12
242  */
243 int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results);
244 
245 /*!
246  * \brief Return the first result node of an XPath query
247  * \param results The XPath results object to get the first result from
248  * \retval The first result in the XPath object on success
249  * \retval NULL on error
250  *
251  * \since 12
252  */
253 struct ast_xml_node *ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results);
254 
255 /*!
256  * \brief Execute an XPath query on an XML document
257  * \param doc The XML document to query
258  * \param xpath_str The XPath query string to execute on the document
259  * \retval An object containing the results of the XPath query on success
260  * \retval NULL on failure
261  *
262  * \since 12
263  */
264 struct ast_xml_xpath_results *ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str);
265 
266 #endif /* _ASTERISK_XML_H */
Definition: test_heap.c:38
int ast_xml_init(void)
Initialize the XML library implementation. This function is used to setup everything needed to start ...
Definition: xml.c:46
const char * ast_xml_get_ns_href(struct ast_xml_ns *ns)
Definition: xml.c:312
struct ast_xml_doc * ast_xml_open(char *filename)
Open an XML document.
Definition: xml.c:63
struct ast_xml_doc * ast_xml_new(void)
Create a XML document.
Definition: xml.c:105
void ast_xml_free_node(struct ast_xml_node *node)
Free node.
Definition: xml.c:212
struct ast_xml_node * ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
Add a child node, to a specified parent node.
Definition: xml.c:137
int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
Dump the specified document to a file.
Definition: xml.c:335
int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
Return the number of results from an XPath query.
Definition: xml.c:375
char * text
Definition: app_queue.c:1508
struct ast_xml_node * ast_xml_get_root(struct ast_xml_doc *doc)
Get the document root node.
Definition: xml.c:199
int value
Definition: syslog.c:37
struct ast_xml_node * ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
Add a child node inside a passed parent node.
Definition: xml.c:125
struct ast_xml_doc * ast_xml_get_doc(struct ast_xml_node *node)
Get the document based on a node.
Definition: xml.c:298
const char * ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
Get a node attribute by name.
Definition: xml.c:236
void ast_xml_free_attr(const char *attribute)
Free an attribute returned by ast_xml_get_attribute()
Definition: xml.c:222
struct ast_xml_node * ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
Add a list of child nodes, to a specified parent node.
Definition: xml.c:145
struct ast_xml_node * ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
Find a node element by name.
Definition: xml.c:266
struct ast_xml_node * ast_xml_new_node(const char *name)
Create a XML node.
Definition: xml.c:113
struct ast_xml_xpath_results * ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
Execute an XPath query on an XML document.
Definition: xml.c:380
void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
Specify the root node of a XML document.
Definition: xml.c:190
void ast_xml_set_text(struct ast_xml_node *node, const char *content)
Set an element content string.
Definition: xml.c:326
struct ast_xml_node * ast_xml_node_get_parent(struct ast_xml_node *node)
Get the parent of a specified node.
Definition: xml.c:360
static const char name[]
Definition: cdr_mysql.c:74
struct ast_xml_node * ast_xml_copy_node_list(struct ast_xml_node *list)
Create a copy of a n ode list.
Definition: xml.c:153
void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
Free the XPath results.
Definition: xml.c:370
void ast_xml_close(struct ast_xml_doc *doc)
Close an already open document and free the used structure.
Definition: xml.c:180
int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
Set an attribute to a node.
Definition: xml.c:253
const char * ast_xml_get_text(struct ast_xml_node *node)
Get an element content string.
Definition: xml.c:317
struct ast_xml_node * ast_xml_node_get_prev(struct ast_xml_node *node)
Get the previous node in the same leve.
Definition: xml.c:355
struct ast_xml_doc * ast_xml_read_memory(char *buffer, size_t size)
Open an XML document that resides in memory.
Definition: xml.c:161
struct ast_xml_ns * ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name)
Definition: xml.c:307
struct ast_xml_node * ast_xml_node_get_next(struct ast_xml_node *node)
Get the next node in the same level.
Definition: xml.c:350
int ast_xml_finish(void)
Cleanup library allocated global data.
Definition: xml.c:53
struct ast_xml_node * ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
Return the first result node of an XPath query.
Definition: xml.c:365
struct ast_xml_node * ast_xml_node_get_children(struct ast_xml_node *node)
Get the node&#39;s children.
Definition: xml.c:345
const char * ast_xml_node_get_name(struct ast_xml_node *node)
Get the name of a node.
Definition: xml.c:340
void ast_xml_free_text(const char *text)
Free a content element that was returned by ast_xml_get_text()
Definition: xml.c:229