ttxml.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef CCAN_TTXML_H
  2. #define CCAN_TTXML_H
  3. /**
  4. * ttxml - tiny XML library for parsing (trusted!) XML documents.
  5. *
  6. * This parses an XML file into a convenient data structure.
  7. *
  8. * Example:
  9. * #include <ccan/ttxml/ttxml.h>
  10. * #include <stdio.h>
  11. *
  12. * int main(int argc, char *argv[])
  13. * {
  14. * XmlNode *xml, *tmp;
  15. *
  16. * xml = xml_load("./test/test.xml2");
  17. * if(!xml)return 1;
  18. *
  19. * tmp = xml_find(xml, "childnode");
  20. *
  21. * printf("%s: %s\n", xml->name, xml_attr(tmp, "attribute"));
  22. *
  23. * xml_free(xml);
  24. *
  25. * return 0;
  26. * }
  27. *
  28. * Licensed under GPL - see LICENSE file for details.
  29. * Author: Daniel Burke <dan.p.burke@gmail.com>
  30. */
  31. /* Every node is one of these */
  32. typedef struct XmlNode {
  33. char * name;
  34. char ** attrib;
  35. int nattrib;
  36. struct XmlNode * child;
  37. struct XmlNode * next;
  38. } XmlNode;
  39. /* It's all pretty straight forward except for the attrib.
  40. *
  41. * Attrib is an array of char*, that is 2x the size of nattrib.
  42. * Each pair of char* points to the attribute name & the attribute value,
  43. * if present.
  44. *
  45. * If it's a text node, then name = "text", and attrib[1] = the body of text.
  46. * This is the only case where there will be an attribute with a null name.
  47. */
  48. XmlNode* xml_load(const char * filename);
  49. void xml_free(XmlNode *target);
  50. char* xml_attr(XmlNode *x, const char *name);
  51. XmlNode * xml_find(XmlNode *xml, const char *name);
  52. #endif /* CCAN_TTXML_H */