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.

96 lines
2.8KB

  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 attribute to bible tag */
  25. xmlNewProp(bibletag, BAD_CAST "nombre", BAD_CAST config->bible);
  26. /* add dtd */
  27. xmlCreateIntSubset(doc, BAD_CAST "Biblia", NULL, BAD_CAST "generarxml.dtd");
  28. booktag = xmlNewNode(NULL, BAD_CAST "Libro");
  29. xmlAddChild(bibletag, booktag);
  30. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  31. text = xmlNewText(BAD_CAST config->book);
  32. xmlAddChild(node, text);
  33. xmlAddChild(booktag, node);
  34. for (i = 0; i < book->chapters; i++) {
  35. chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo");
  36. xmlAddChild(booktag, chaptertag);
  37. length = snprintf(NULL, 0, "%d", book->chapter[i]->chapter) + strlen(config->chapter);
  38. temp = (char *) malloc((length + 2) * sizeof(char));
  39. snprintf(temp, length + 2, "%s %d", config->chapter, book->chapter[i]->chapter);
  40. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  41. text = xmlNewText(BAD_CAST temp);
  42. xmlAddChild(node, text);
  43. xmlAddChild(chaptertag, node);
  44. free(temp);
  45. chapter = book->chapter[i];
  46. chapter->current = 0;
  47. for (chapter->current = 0; chapter->current < chapter->verses; chapter->current++) {
  48. versetag = xmlNewNode(NULL, BAD_CAST "Versiculo");
  49. xmlAddChild(chaptertag, versetag);
  50. length = snprintf(NULL, 0, "%d", chapter->current + 1) + strlen("VERSICULO");
  51. temp = (char *) malloc((length + 2) * sizeof(char));
  52. snprintf(temp, length + 2, "VERSICULO %d", chapter->current + 1);
  53. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  54. text = xmlNewText(BAD_CAST temp);
  55. xmlAddChild(node, text);
  56. xmlAddChild(versetag, node);
  57. free(temp);
  58. node = xmlNewNode(NULL, BAD_CAST "Descripcion");
  59. text = xmlNewText(BAD_CAST chapter->verse[chapter->current]);
  60. xmlAddChild(node, text);
  61. xmlAddChild(versetag, node);
  62. }
  63. }
  64. #ifdef DEBUG
  65. printf("XML output:\n");
  66. xmlSaveFormatFileEnc("-", doc, "UTF-8", 1);
  67. #endif
  68. xmlSaveFormatFileEnc(config->file, doc, "UTF-8", 1);
  69. /* cleanup on aisle 3 */
  70. xmlFreeDoc(doc);
  71. xmlCleanupParser();
  72. xmlMemoryDump();
  73. return 0;
  74. }