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.

makexml.c 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 length;
  10. char *temp = NULL;
  11. xmlDocPtr doc = NULL;
  12. xmlNodePtr bibletag = NULL;
  13. xmlNodePtr booktag = NULL;
  14. xmlNodePtr chaptertag = NULL;
  15. xmlNodePtr versetag = NULL;
  16. xmlNodePtr node = NULL;
  17. xmlNodePtr text = NULL;
  18. LIBXML_TEST_VERSION;
  19. doc = xmlNewDoc(BAD_CAST "1.0");
  20. bibletag = xmlNewNode(NULL, BAD_CAST "Biblia");
  21. xmlDocSetRootElement(doc, bibletag);
  22. /* add dtd */
  23. xmlCreateIntSubset(doc, BAD_CAST "Biblia", NULL, BAD_CAST "generarxml.dtd");
  24. booktag = xmlNewNode(NULL, BAD_CAST "Libro");
  25. xmlAddChild(bibletag, booktag);
  26. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  27. text = xmlNewText(BAD_CAST config->book);
  28. xmlAddChild(node, text);
  29. xmlAddChild(booktag, node);
  30. for (i = 0; i < book->chapters; i++) {
  31. chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo");
  32. xmlAddChild(booktag, chaptertag);
  33. length = snprintf(NULL, 0, "%d", book->chapter[i]->chapter) + strlen(config->chapter);
  34. temp = (char *) malloc((length + 2) * sizeof(char));
  35. snprintf(temp, length + 2, "%s %d", config->chapter, book->chapter[i]->chapter);
  36. node = xmlNewNode(NULL, BAD_CAST "Nombre");
  37. text = xmlNewText(BAD_CAST temp);
  38. xmlAddChild(node, text);
  39. xmlAddChild(chaptertag, node);
  40. versetag = xmlNewNode(NULL, BAD_CAST "Versiculo");
  41. xmlAddChild(chaptertag, versetag);
  42. }
  43. #ifdef DEBUG
  44. printf("XML output:\n");
  45. xmlSaveFormatFileEnc("-", doc, "UTF-8", 1);
  46. #endif
  47. xmlSaveFormatFileEnc(config->file, doc, "UTF-8", 1);
  48. /* cleanup on aisle 3 */
  49. xmlFreeDoc(doc);
  50. xmlCleanupParser();
  51. xmlMemoryDump();
  52. return 0;
  53. }