xml.h 8.0 KB

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