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.

92 lines
2.7KB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <libxml/parser.h>
  5. #include <libxml/tree.h>
  6. #include "main.h"
  7. int makexml() {
  8. int i = 0;
  9. int j = 0;
  10. int length;
  11. char *temp = NULL;
  12. CHAPTER *chapter = NULL;
  13. xmlDocPtr doc = NULL;
  14. xmlNodePtr bibletag = NULL;
  15. xmlNodePtr booktag = NULL;
  16. xmlNodePtr chaptertag = NULL;
  17. xmlNodePtr versetag = NULL;
  18. xmlNodePtr node = NULL;
  19. xmlNodePtr text = NULL;
  20. LIBXML_TEST_VERSION;
  21. doc = xmlNewDoc(BAD_CAST "1.0");
  22. bibletag = xmlNewNode(NULL, BAD_CAST "Biblia");
  23. xmlDocSetRootElement(doc, bibletag);
  24. /* add dtd */
  25. xmlCreateIntSubset(doc, BAD_CAST "Biblia", NULL, BAD_CAST "generarxml.dtd");
  26. booktag = xmlNewNode(NULL, BAD_CAST "Libro");
  27. xmlAddChild(bibletag, booktag);
  28. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  29. text = xmlNewText(BAD_CAST config->book);
  30. xmlAddChild(node, text);
  31. xmlAddChild(booktag, node);
  32. for (i = 0; i < book->chapters; i++) {
  33. chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo");
  34. xmlAddChild(booktag, chaptertag);
  35. length = snprintf(NULL, 0, "%d", book->chapter[i]->chapter) + strlen(config->chapter);
  36. temp = (char *) malloc((length + 2) * sizeof(char));
  37. snprintf(temp, length + 2, "%s %d", config->chapter, book->chapter[i]->chapter);
  38. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  39. text = xmlNewText(BAD_CAST temp);
  40. xmlAddChild(node, text);
  41. xmlAddChild(chaptertag, node);
  42. free(temp);
  43. chapter = book->chapter[i];
  44. chapter->current = 0;
  45. for (chapter->current = 0; chapter->current < chapter->verses; chapter->current++) {
  46. versetag = xmlNewNode(NULL, BAD_CAST "Versiculo");
  47. xmlAddChild(chaptertag, versetag);
  48. length = snprintf(NULL, 0, "%d", chapter->current + 1) + strlen("VERSICULO");
  49. temp = (char *) malloc((length + 2) * sizeof(char));
  50. snprintf(temp, length + 2, "VERSICULO %d", chapter->current + 1);
  51. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  52. text = xmlNewText(BAD_CAST temp);
  53. xmlAddChild(node, text);
  54. xmlAddChild(versetag, node);
  55. free(temp);
  56. node = xmlNewNode(NULL, BAD_CAST "Descripcion");
  57. text = xmlNewText(BAD_CAST chapter->verse[chapter->current]);
  58. xmlAddChild(node, text);
  59. xmlAddChild(versetag, node);
  60. }
  61. }
  62. #ifdef DEBUG
  63. printf("XML output:\n");
  64. xmlSaveFormatFileEnc("-", doc, "UTF-8", 1);
  65. #endif
  66. xmlSaveFormatFileEnc(config->file, doc, "UTF-8", 1);
  67. /* cleanup on aisle 3 */
  68. xmlFreeDoc(doc);
  69. xmlCleanupParser();
  70. xmlMemoryDump();
  71. return 0;
  72. }