reader2.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. program reader2;
  15. {$mode objfpc}
  16. uses
  17. ctypes,
  18. xml2,
  19. exutils;
  20. (**
  21. * processNode:
  22. * @reader: the xmlReader
  23. *
  24. * Dump information about the current node
  25. *)
  26. procedure processNode(reader: xmlTextReaderPtr);
  27. var
  28. value, name: xmlCharPtr;
  29. begin
  30. name := xmlTextReaderConstName(reader);
  31. if not assigned(name) then
  32. name := pchar('--'); // BAD_CAST !!!!
  33. value := xmlTextReaderConstValue(reader);
  34. printf('%d %d %s %d %d',
  35. [xmlTextReaderDepth(reader),
  36. xmlTextReaderNodeType(reader),
  37. name,
  38. xmlTextReaderIsEmptyElement(reader),
  39. xmlTextReaderHasValue(reader)]);
  40. if not assigned(value) then
  41. writeln
  42. else
  43. if (xmlStrlen(value) > 40) then
  44. printfn(' %.40s...\n', [value])
  45. else
  46. printfn(' %s\n', [value]);
  47. end;
  48. (**
  49. * streamFile:
  50. * @filename: the file name to parse
  51. *
  52. * Parse and print information about an XML file.
  53. *)
  54. procedure streamFile(const filename: pchar);
  55. var
  56. reader: xmlTextReaderPtr;
  57. ret: cint;
  58. begin
  59. (*
  60. * Pass some special parsing options to activate DTD attribute defaulting,
  61. * entities substitution and DTD validation
  62. *)
  63. reader := xmlReaderForFile(filename, nil,
  64. XML_PARSE_DTDATTR or // default DTD attributes
  65. XML_PARSE_NOENT or // substitute entities
  66. XML_PARSE_DTDVALID); // validate with the DTD
  67. if assigned(reader) then
  68. begin
  69. ret := xmlTextReaderRead(reader);
  70. while ret = 1 do
  71. begin
  72. processNode(reader);
  73. ret := xmlTextReaderRead(reader);
  74. end;
  75. (*
  76. * Once the document has been fully parsed check the validation results
  77. *)
  78. if (xmlTextReaderIsValid(reader) <> 1) then
  79. printfn('Document %s does not validate', [filename])
  80. else begin
  81. xmlFreeTextReader(reader);
  82. if ret <> 0 then
  83. printfn('%s : failed to parse', [filename]);
  84. end;
  85. end else
  86. printfn('Unable to open %s', [filename]);
  87. end;
  88. begin
  89. (*
  90. * this initialize the library and check potential ABI mismatches
  91. * between the version it was compiled for and the actual shared
  92. * library used.
  93. *)
  94. LIBXML_TEST_VERSION;
  95. streamFile(pchar(ParamStr(1)));
  96. (*
  97. * Cleanup function for the XML library.
  98. *)
  99. xmlCleanupParser();
  100. end.