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