reader2.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * section: xmlReader
  3. * synopsis: Parse and validate an XML file with an xmlReader
  4. * purpose: Demonstrate the use of xmlReaderForFile() to parse an XML file
  5. * validating the content in the process and activating options
  6. * like entities substitution, and DTD attributes defaulting.
  7. * (Note that the XMLReader functions require libxml2 version later
  8. * than 2.6.)
  9. * usage: reader2 <valid_xml_filename>
  10. * test: reader2 test2.xml > reader1.tmp ; diff reader1.tmp reader1.res ; rm reader1.tmp
  11. * author: Daniel Veillard
  12. * copy: see Copyright for the status of this software.
  13. */
  14. #include <stdio.h>
  15. #include <libxml/xmlreader.h>
  16. #ifdef LIBXML_READER_ENABLED
  17. /**
  18. * processNode:
  19. * @reader: the xmlReader
  20. *
  21. * Dump information about the current node
  22. */
  23. static void
  24. processNode(xmlTextReaderPtr reader) {
  25. const xmlChar *name, *value;
  26. name = xmlTextReaderConstName(reader);
  27. if (name == NULL)
  28. name = BAD_CAST "--";
  29. value = xmlTextReaderConstValue(reader);
  30. printf("%d %d %s %d %d",
  31. xmlTextReaderDepth(reader),
  32. xmlTextReaderNodeType(reader),
  33. name,
  34. xmlTextReaderIsEmptyElement(reader),
  35. xmlTextReaderHasValue(reader));
  36. if (value == NULL)
  37. printf("\n");
  38. else {
  39. if (xmlStrlen(value) > 40)
  40. printf(" %.40s...\n", value);
  41. else
  42. printf(" %s\n", value);
  43. }
  44. }
  45. /**
  46. * streamFile:
  47. * @filename: the file name to parse
  48. *
  49. * Parse, validate and print information about an XML file.
  50. */
  51. static void
  52. streamFile(const char *filename) {
  53. xmlTextReaderPtr reader;
  54. int ret;
  55. /*
  56. * Pass some special parsing options to activate DTD attribute defaulting,
  57. * entities substitution and DTD validation
  58. */
  59. reader = xmlReaderForFile(filename, NULL,
  60. XML_PARSE_DTDATTR | /* default DTD attributes */
  61. XML_PARSE_NOENT | /* substitute entities */
  62. XML_PARSE_DTDVALID); /* validate with the DTD */
  63. if (reader != NULL) {
  64. ret = xmlTextReaderRead(reader);
  65. while (ret == 1) {
  66. processNode(reader);
  67. ret = xmlTextReaderRead(reader);
  68. }
  69. /*
  70. * Once the document has been fully parsed check the validation results
  71. */
  72. if (xmlTextReaderIsValid(reader) != 1) {
  73. fprintf(stderr, "Document %s does not validate\n", filename);
  74. }
  75. xmlFreeTextReader(reader);
  76. if (ret != 0) {
  77. fprintf(stderr, "%s : failed to parse\n", filename);
  78. }
  79. } else {
  80. fprintf(stderr, "Unable to open %s\n", filename);
  81. }
  82. }
  83. int main(int argc, char **argv) {
  84. if (argc != 2)
  85. return(1);
  86. /*
  87. * this initialize the library and check potential ABI mismatches
  88. * between the version it was compiled for and the actual shared
  89. * library used.
  90. */
  91. LIBXML_TEST_VERSION
  92. streamFile(argv[1]);
  93. /*
  94. * Cleanup function for the XML library.
  95. */
  96. xmlCleanupParser();
  97. /*
  98. * this is to debug memory for regression tests
  99. */
  100. xmlMemoryDump();
  101. return(0);
  102. }
  103. #else
  104. int main(void) {
  105. fprintf(stderr, "XInclude support not compiled in\n");
  106. exit(1);
  107. }
  108. #endif