Program to generate an xml file based on a provided text file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4KB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <libxml/parser.h>
  4. #include <libxml/tree.h>
  5. #include <libxml/xmlIO.h>
  6. #include <libxml/xinclude.h>
  7. #ifdef LIBXML_TREE_ENABLED
  8. static void
  9. print_element_names(xmlNode * a_node)
  10. {
  11. xmlNode *cur_node = NULL;
  12. for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
  13. if (cur_node->type == XML_ELEMENT_NODE) {
  14. printf("node type: Element, name: %s\n", cur_node->name);
  15. }
  16. print_element_names(cur_node->children);
  17. }
  18. }
  19. int readconfig(char *config_file) {
  20. /* Initilize the library */
  21. LIBXML_TEST_VERSION
  22. xmlParserCtxtPtr context;
  23. xmlDocPtr config = NULL;
  24. xmlNodePtr root_element = NULL;
  25. context = xmlNewParserCtxt();
  26. if (context == NULL) {
  27. fprintf(stderr, "No pudo alocar contexto de analizador!\n");
  28. return 1;
  29. }
  30. config = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
  31. if (config == NULL) {
  32. fprintf(stderr, "Falló analizar %s\n", config_file);
  33. }
  34. else {
  35. if (context->valid == 0) {
  36. fprintf(stderr, "Falló validar %s\n", config_file);
  37. }
  38. root_element = xmlDocGetRootElement(config);
  39. print_element_names(root_element);
  40. xmlFreeDoc(config);
  41. }
  42. xmlFreeParserCtxt(context);
  43. return 0;
  44. }
  45. #else
  46. int readconfig(char *config_file) {
  47. fprintf(stderr, "libxml2 no tiene tree support compilado\n");
  48. return 1;
  49. }
  50. #endif