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.9 KiB

  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 makexml() {
  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 bibletag = NULL;
  18. xmlNodePtr booktag = NULL;
  19. xmlNodePtr chaptertag = NULL;
  20. xmlNodePtr versetag = NULL;
  21. xmlNodePtr node = NULL;
  22. xmlNodePtr text = NULL;
  23. LIBXML_TEST_VERSION;
  24. doc = xmlNewDoc(BAD_CAST "1.0");
  25. bibletag = xmlNewNode(NULL, BAD_CAST "Biblia");
  26. xmlDocSetRootElement(doc, bibletag);
  27. /* add attribute to bible tag */
  28. xmlNewProp(bibletag, BAD_CAST "nombre", BAD_CAST config->bible);
  29. /* add dtd */
  30. xmlCreateIntSubset(doc, BAD_CAST "Biblia", NULL, BAD_CAST "generarxml.dtd");
  31. booktag = xmlNewNode(NULL, BAD_CAST "Libro");
  32. xmlAddChild(bibletag, booktag);
  33. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  34. text = xmlNewText(BAD_CAST config->book);
  35. xmlAddChild(node, text);
  36. xmlAddChild(booktag, node);
  37. /* add the chapters */
  38. for (i = 0; i < book->chapters; i++) {
  39. chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo");
  40. xmlAddChild(booktag, chaptertag);
  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(chaptertag, 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. versetag = xmlNewNode(NULL, BAD_CAST "Versiculo");
  54. xmlAddChild(chaptertag, versetag);
  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(versetag, 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(versetag, 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. }