Annotation of embedaddon/libxml2/doc/examples/parse2.c, revision 1.1
1.1 ! misho 1: /**
! 2: * section: Parsing
! 3: * synopsis: Parse and validate an XML file to a tree and free the result
! 4: * purpose: Create a parser context for an XML file, then parse and validate
! 5: * the file, creating a tree, check the validation result
! 6: * and xmlFreeDoc() to free the resulting tree.
! 7: * usage: parse2 test2.xml
! 8: * test: parse2 test2.xml
! 9: * author: Daniel Veillard
! 10: * copy: see Copyright for the status of this software.
! 11: */
! 12:
! 13: #include <stdio.h>
! 14: #include <libxml/parser.h>
! 15: #include <libxml/tree.h>
! 16:
! 17: /**
! 18: * exampleFunc:
! 19: * @filename: a filename or an URL
! 20: *
! 21: * Parse and validate the resource and free the resulting tree
! 22: */
! 23: static void
! 24: exampleFunc(const char *filename) {
! 25: xmlParserCtxtPtr ctxt; /* the parser context */
! 26: xmlDocPtr doc; /* the resulting document tree */
! 27:
! 28: /* create a parser context */
! 29: ctxt = xmlNewParserCtxt();
! 30: if (ctxt == NULL) {
! 31: fprintf(stderr, "Failed to allocate parser context\n");
! 32: return;
! 33: }
! 34: /* parse the file, activating the DTD validation option */
! 35: doc = xmlCtxtReadFile(ctxt, filename, NULL, XML_PARSE_DTDVALID);
! 36: /* check if parsing suceeded */
! 37: if (doc == NULL) {
! 38: fprintf(stderr, "Failed to parse %s\n", filename);
! 39: } else {
! 40: /* check if validation suceeded */
! 41: if (ctxt->valid == 0)
! 42: fprintf(stderr, "Failed to validate %s\n", filename);
! 43: /* free up the resulting document */
! 44: xmlFreeDoc(doc);
! 45: }
! 46: /* free up the parser context */
! 47: xmlFreeParserCtxt(ctxt);
! 48: }
! 49:
! 50: int main(int argc, char **argv) {
! 51: if (argc != 2)
! 52: return(1);
! 53:
! 54: /*
! 55: * this initialize the library and check potential ABI mismatches
! 56: * between the version it was compiled for and the actual shared
! 57: * library used.
! 58: */
! 59: LIBXML_TEST_VERSION
! 60:
! 61: exampleFunc(argv[1]);
! 62:
! 63: /*
! 64: * Cleanup function for the XML library.
! 65: */
! 66: xmlCleanupParser();
! 67: /*
! 68: * this is to debug memory for regression tests
! 69: */
! 70: xmlMemoryDump();
! 71: return(0);
! 72: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>