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.

98 lines
2.9KB

  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. /*
  8. * This function generates the xml and saves it to the output file.
  9. */
  10. void make_xml() {
  11. int i = 0;
  12. int j = 0;
  13. int length;
  14. char *temp = NULL;
  15. CHAPTER *chapter = NULL;
  16. xmlDocPtr doc = NULL;
  17. xmlNodePtr bible_tag = NULL;
  18. xmlNodePtr book_tag = NULL;
  19. xmlNodePtr chapter_tag = NULL;
  20. xmlNodePtr verse_tag = NULL;
  21. xmlNodePtr node = NULL;
  22. xmlNodePtr text = NULL;
  23. LIBXML_TEST_VERSION;
  24. doc = xmlNewDoc(BAD_CAST "1.0");
  25. bible_tag = xmlNewNode(NULL, BAD_CAST "Biblia");
  26. xmlDocSetRootElement(doc, bible_tag);
  27. /* add attribute to bible tag */
  28. xmlNewProp(bible_tag, BAD_CAST "nombre", BAD_CAST config->bible);
  29. /* add dtd */
  30. xmlCreateIntSubset(doc, BAD_CAST "Biblia", NULL, BAD_CAST "generarxml.dtd");
  31. book_tag = xmlNewNode(NULL, BAD_CAST "Libro");
  32. xmlAddChild(bible_tag, book_tag);
  33. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  34. text = xmlNewText(BAD_CAST config->book);
  35. xmlAddChild(node, text);
  36. xmlAddChild(book_tag, node);
  37. /* add the chapters */
  38. for (i = 0; i < book->chapters; i++) {
  39. chapter_tag = xmlNewNode(NULL, BAD_CAST "Capitulo");
  40. xmlAddChild(book_tag, chapter_tag);
  41. length = snprintf(NULL, 0, "%d", book->chapter[i]->chapter) + strlen(config->chapter);
  42. temp = (char *) malloc((length + 2) * sizeof(char));
  43. snprintf(temp, length + 2, "%s %d", config->chapter, book->chapter[i]->chapter);
  44. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  45. text = xmlNewText(BAD_CAST temp);
  46. xmlAddChild(node, text);
  47. xmlAddChild(chapter_tag, node);
  48. free(temp);
  49. /* add the verses */
  50. chapter = book->chapter[i];
  51. chapter->current = 0;
  52. for (chapter->current = 0; chapter->current < chapter->verses; chapter->current++) {
  53. verse_tag = xmlNewNode(NULL, BAD_CAST "Versiculo");
  54. xmlAddChild(chapter_tag, verse_tag);
  55. length = snprintf(NULL, 0, "%d", chapter->current + 1) + strlen("VERSICULO");
  56. temp = (char *) malloc((length + 2) * sizeof(char));
  57. snprintf(temp, length + 2, "VERSICULO %d", chapter->current + 1);
  58. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  59. text = xmlNewText(BAD_CAST temp);
  60. xmlAddChild(node, text);
  61. xmlAddChild(verse_tag, node);
  62. free(temp);
  63. node = xmlNewNode(NULL, BAD_CAST "Descripcion");
  64. text = xmlNewText(BAD_CAST chapter->verse[chapter->current]);
  65. xmlAddChild(node, text);
  66. xmlAddChild(verse_tag, node);
  67. }
  68. }
  69. #ifdef DEBUG
  70. printf("XML output:\n");
  71. xmlSaveFormatFileEnc("-", doc, "UTF-8", 1);
  72. #endif
  73. xmlSaveFormatFileEnc(config->file, doc, "UTF-8", 1);
  74. /* cleanup on aisle 3 */
  75. xmlFreeDoc(doc);
  76. xmlCleanupParser();
  77. xmlMemoryDump();
  78. }