Blame doc/examples/parse4.c

Packit Service a31ea6
/**
Packit Service a31ea6
 * section: Parsing
Packit Service a31ea6
 * synopsis: Parse an XML document chunk by chunk to a tree and free it
Packit Service a31ea6
 * purpose: Demonstrate the use of xmlCreatePushParserCtxt() and
Packit Service a31ea6
 *          xmlParseChunk() to read an XML file progressively
Packit Service a31ea6
 *          into a tree and and xmlFreeDoc() to free the resulting tree
Packit Service a31ea6
 * usage: parse4 test3.xml
Packit Service a31ea6
 * test: parse4 test3.xml
Packit Service a31ea6
 * author: Daniel Veillard
Packit Service a31ea6
 * copy: see Copyright for the status of this software.
Packit Service a31ea6
 */
Packit Service a31ea6
Packit Service a31ea6
#include <stdio.h>
Packit Service a31ea6
#include <libxml/parser.h>
Packit Service a31ea6
#include <libxml/tree.h>
Packit Service a31ea6
Packit Service a31ea6
#ifdef LIBXML_PUSH_ENABLED
Packit Service a31ea6
static FILE *desc;
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * readPacket:
Packit Service a31ea6
 * @mem: array to store the packet
Packit Service a31ea6
 * @size: the packet size
Packit Service a31ea6
 *
Packit Service a31ea6
 * read at most @size bytes from the document and store it in @mem
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the number of bytes read
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
readPacket(char *mem, int size) {
Packit Service a31ea6
    int res;
Packit Service a31ea6
Packit Service a31ea6
    res = fread(mem, 1, size, desc);
Packit Service a31ea6
    return(res);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * example4Func:
Packit Service a31ea6
 * @filename: a filename or an URL
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parse the resource and free the resulting tree
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
example4Func(const char *filename) {
Packit Service a31ea6
    xmlParserCtxtPtr ctxt;
Packit Service a31ea6
    char chars[4];
Packit Service a31ea6
    xmlDocPtr doc; /* the resulting document tree */
Packit Service a31ea6
    int res;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Read a few first byte to check the input used for the
Packit Service a31ea6
     * encoding detection at the parser level.
Packit Service a31ea6
     */
Packit Service a31ea6
    res = readPacket(chars, 4);
Packit Service a31ea6
    if (res <= 0) {
Packit Service a31ea6
        fprintf(stderr, "Failed to parse %s\n", filename);
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Create a progressive parsing context, the 2 first arguments
Packit Service a31ea6
     * are not used since we want to build a tree and not use a SAX
Packit Service a31ea6
     * parsing interface. We also pass the first bytes of the document
Packit Service a31ea6
     * to allow encoding detection when creating the parser but this
Packit Service a31ea6
     * is optional.
Packit Service a31ea6
     */
Packit Service a31ea6
    ctxt = xmlCreatePushParserCtxt(NULL, NULL,
Packit Service a31ea6
                                   chars, res, filename);
Packit Service a31ea6
    if (ctxt == NULL) {
Packit Service a31ea6
        fprintf(stderr, "Failed to create parser context !\n");
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * loop on the input getting the document data, of course 4 bytes
Packit Service a31ea6
     * at a time is not realistic but allows to verify testing on small
Packit Service a31ea6
     * documents.
Packit Service a31ea6
     */
Packit Service a31ea6
    while ((res = readPacket(chars, 4)) > 0) {
Packit Service a31ea6
        xmlParseChunk(ctxt, chars, res, 0);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * there is no more input, indicate the parsing is finished.
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlParseChunk(ctxt, chars, 0, 1);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * collect the document back and if it was wellformed
Packit Service a31ea6
     * and destroy the parser context.
Packit Service a31ea6
     */
Packit Service a31ea6
    doc = ctxt->myDoc;
Packit Service a31ea6
    res = ctxt->wellFormed;
Packit Service a31ea6
    xmlFreeParserCtxt(ctxt);
Packit Service a31ea6
Packit Service a31ea6
    if (!res) {
Packit Service a31ea6
        fprintf(stderr, "Failed to parse %s\n", filename);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * since we don't use the document, destroy it now.
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlFreeDoc(doc);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
int main(int argc, char **argv) {
Packit Service a31ea6
    if (argc != 2)
Packit Service a31ea6
        return(1);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * this initialize the library and check potential ABI mismatches
Packit Service a31ea6
     * between the version it was compiled for and the actual shared
Packit Service a31ea6
     * library used.
Packit Service a31ea6
     */
Packit Service a31ea6
    LIBXML_TEST_VERSION
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * simulate a progressive parsing using the input file.
Packit Service a31ea6
     */
Packit Service a31ea6
    desc = fopen(argv[1], "rb");
Packit Service a31ea6
    if (desc != NULL) {
Packit Service a31ea6
	example4Func(argv[1]);
Packit Service a31ea6
	fclose(desc);
Packit Service a31ea6
    } else {
Packit Service a31ea6
        fprintf(stderr, "Failed to parse %s\n", argv[1]);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Cleanup function for the XML library.
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlCleanupParser();
Packit Service a31ea6
    /*
Packit Service a31ea6
     * this is to debug memory for regression tests
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlMemoryDump();
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
#else /* ! LIBXML_PUSH_ENABLED */
Packit Service a31ea6
int main(int argc, char **argv) {
Packit Service a31ea6
    fprintf(stderr, "Library not compiled with push parser support\n");
Packit Service a31ea6
    return(1);
Packit Service a31ea6
}
Packit Service a31ea6
#endif