Blame doc/examples/parse2.c

Packit Service a31ea6
/**
Packit Service a31ea6
 * section: Parsing
Packit Service a31ea6
 * synopsis: Parse and validate an XML file to a tree and free the result
Packit Service a31ea6
 * purpose: Create a parser context for an XML file, then parse and validate
Packit Service a31ea6
 *          the file, creating a tree, check the validation result
Packit Service a31ea6
 *          and xmlFreeDoc() to free the resulting tree.
Packit Service a31ea6
 * usage: parse2 test2.xml
Packit Service a31ea6
 * test: parse2 test2.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
/**
Packit Service a31ea6
 * exampleFunc:
Packit Service a31ea6
 * @filename: a filename or an URL
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parse and validate the resource and free the resulting tree
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
exampleFunc(const char *filename) {
Packit Service a31ea6
    xmlParserCtxtPtr ctxt; /* the parser context */
Packit Service a31ea6
    xmlDocPtr doc; /* the resulting document tree */
Packit Service a31ea6
Packit Service a31ea6
    /* create a parser context */
Packit Service a31ea6
    ctxt = xmlNewParserCtxt();
Packit Service a31ea6
    if (ctxt == NULL) {
Packit Service a31ea6
        fprintf(stderr, "Failed to allocate parser context\n");
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
    /* parse the file, activating the DTD validation option */
Packit Service a31ea6
    doc = xmlCtxtReadFile(ctxt, filename, NULL, XML_PARSE_DTDVALID);
Packit Service a31ea6
    /* check if parsing suceeded */
Packit Service a31ea6
    if (doc == NULL) {
Packit Service a31ea6
        fprintf(stderr, "Failed to parse %s\n", filename);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	/* check if validation suceeded */
Packit Service a31ea6
        if (ctxt->valid == 0)
Packit Service a31ea6
	    fprintf(stderr, "Failed to validate %s\n", filename);
Packit Service a31ea6
	/* free up the resulting document */
Packit Service a31ea6
	xmlFreeDoc(doc);
Packit Service a31ea6
    }
Packit Service a31ea6
    /* free up the parser context */
Packit Service a31ea6
    xmlFreeParserCtxt(ctxt);
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
    exampleFunc(argv[1]);
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
}