Blame doc/examples/reader4.c

Packit 423ecb
/**
Packit 423ecb
 * section: xmlReader
Packit 423ecb
 * synopsis: Parse multiple XML files reusing an xmlReader
Packit 423ecb
 * purpose: Demonstrate the use of xmlReaderForFile() and
Packit 423ecb
 * xmlReaderNewFile to parse XML files while reusing the reader object
Packit 423ecb
 * and parser context.  (Note that the XMLReader functions require
Packit 423ecb
 * libxml2 version later than 2.6.)
Packit 423ecb
 * usage: reader4 <filename> [ filename ... ]
Packit 423ecb
 * test: reader4 test1.xml test2.xml test3.xml > reader4.tmp && diff reader4.tmp $(srcdir)/reader4.res
Packit 423ecb
 * author: Graham Bennett
Packit 423ecb
 * copy: see Copyright for the status of this software.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#include <stdio.h>
Packit 423ecb
#include <libxml/xmlreader.h>
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_READER_ENABLED
Packit 423ecb
Packit 423ecb
static void processDoc(xmlTextReaderPtr readerPtr) {
Packit 423ecb
    int ret;
Packit 423ecb
    xmlDocPtr docPtr;
Packit 423ecb
    const xmlChar *URL;
Packit 423ecb
Packit 423ecb
    ret = xmlTextReaderRead(readerPtr);
Packit 423ecb
    while (ret == 1) {
Packit 423ecb
      ret = xmlTextReaderRead(readerPtr);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * One can obtain the document pointer to get insteresting
Packit 423ecb
     * information about the document like the URL, but one must also
Packit 423ecb
     * be sure to clean it up at the end (see below).
Packit 423ecb
     */
Packit 423ecb
    docPtr = xmlTextReaderCurrentDoc(readerPtr);
Packit 423ecb
    if (NULL == docPtr) {
Packit 423ecb
      fprintf(stderr, "failed to obtain document\n");      
Packit 423ecb
      return;
Packit 423ecb
    }
Packit 423ecb
      
Packit 423ecb
    URL = docPtr->URL;
Packit 423ecb
    if (NULL == URL) {
Packit 423ecb
      fprintf(stderr, "Failed to obtain URL\n");      
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (ret != 0) {
Packit 423ecb
      fprintf(stderr, "%s: Failed to parse\n", URL);
Packit 423ecb
      return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    printf("%s: Processed ok\n", (const char *)URL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
int main(int argc, char **argv) {
Packit 423ecb
    xmlTextReaderPtr readerPtr;
Packit 423ecb
    int i;
Packit 423ecb
    xmlDocPtr docPtr;
Packit 423ecb
Packit 423ecb
    if (argc < 2)
Packit 423ecb
        return(1);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * this initialises the library and check potential ABI mismatches
Packit 423ecb
     * between the version it was compiled for and the actual shared
Packit 423ecb
     * library used.
Packit 423ecb
     */
Packit 423ecb
    LIBXML_TEST_VERSION
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Create a new reader for the first file and process the
Packit 423ecb
     * document.
Packit 423ecb
     */
Packit 423ecb
    readerPtr = xmlReaderForFile(argv[1], NULL, 0);
Packit 423ecb
    if (NULL == readerPtr) {
Packit 423ecb
      fprintf(stderr, "%s: failed to create reader\n", argv[1]);      
Packit 423ecb
      return(1);
Packit 423ecb
    }
Packit 423ecb
    processDoc(readerPtr);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * The reader can be reused for subsequent files.
Packit 423ecb
     */
Packit 423ecb
    for (i=2; i < argc; ++i) {
Packit 423ecb
      	xmlReaderNewFile(readerPtr, argv[i], NULL, 0);
Packit 423ecb
	if (NULL == readerPtr) {
Packit 423ecb
	  fprintf(stderr, "%s: failed to create reader\n", argv[i]);      
Packit 423ecb
	  return(1);
Packit 423ecb
	}
Packit 423ecb
        processDoc(readerPtr);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Since we've called xmlTextReaderCurrentDoc, we now have to
Packit 423ecb
     * clean up after ourselves.  We only have to do this the last
Packit 423ecb
     * time, because xmlReaderNewFile calls xmlCtxtReset which takes
Packit 423ecb
     * care of it.
Packit 423ecb
     */
Packit 423ecb
    docPtr = xmlTextReaderCurrentDoc(readerPtr);
Packit 423ecb
    if (docPtr != NULL)
Packit 423ecb
      xmlFreeDoc(docPtr);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Clean up the reader.
Packit 423ecb
     */
Packit 423ecb
    xmlFreeTextReader(readerPtr);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Cleanup function for the XML library.
Packit 423ecb
     */
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    /*
Packit 423ecb
     * this is to debug memory for regression tests
Packit 423ecb
     */
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#else
Packit 423ecb
int main(void) {
Packit 423ecb
    fprintf(stderr, "xmlReader support not compiled in\n");
Packit 423ecb
    exit(1);
Packit 423ecb
}
Packit 423ecb
#endif