Return to reader1.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / libxml2 / doc / examples |
1.1 ! misho 1: /** ! 2: * section: xmlReader ! 3: * synopsis: Parse an XML file with an xmlReader ! 4: * purpose: Demonstrate the use of xmlReaderForFile() to parse an XML file ! 5: * and dump the informations about the nodes found in the process. ! 6: * (Note that the XMLReader functions require libxml2 version later ! 7: * than 2.6.) ! 8: * usage: reader1 <filename> ! 9: * test: reader1 test2.xml > reader1.tmp ; diff reader1.tmp reader1.res ; rm reader1.tmp ! 10: * author: Daniel Veillard ! 11: * copy: see Copyright for the status of this software. ! 12: */ ! 13: ! 14: #include <stdio.h> ! 15: #include <libxml/xmlreader.h> ! 16: ! 17: #ifdef LIBXML_READER_ENABLED ! 18: ! 19: /** ! 20: * processNode: ! 21: * @reader: the xmlReader ! 22: * ! 23: * Dump information about the current node ! 24: */ ! 25: static void ! 26: processNode(xmlTextReaderPtr reader) { ! 27: const xmlChar *name, *value; ! 28: ! 29: name = xmlTextReaderConstName(reader); ! 30: if (name == NULL) ! 31: name = BAD_CAST "--"; ! 32: ! 33: value = xmlTextReaderConstValue(reader); ! 34: ! 35: printf("%d %d %s %d %d", ! 36: xmlTextReaderDepth(reader), ! 37: xmlTextReaderNodeType(reader), ! 38: name, ! 39: xmlTextReaderIsEmptyElement(reader), ! 40: xmlTextReaderHasValue(reader)); ! 41: if (value == NULL) ! 42: printf("\n"); ! 43: else { ! 44: if (xmlStrlen(value) > 40) ! 45: printf(" %.40s...\n", value); ! 46: else ! 47: printf(" %s\n", value); ! 48: } ! 49: } ! 50: ! 51: /** ! 52: * streamFile: ! 53: * @filename: the file name to parse ! 54: * ! 55: * Parse and print information about an XML file. ! 56: */ ! 57: static void ! 58: streamFile(const char *filename) { ! 59: xmlTextReaderPtr reader; ! 60: int ret; ! 61: ! 62: reader = xmlReaderForFile(filename, NULL, 0); ! 63: if (reader != NULL) { ! 64: ret = xmlTextReaderRead(reader); ! 65: while (ret == 1) { ! 66: processNode(reader); ! 67: ret = xmlTextReaderRead(reader); ! 68: } ! 69: xmlFreeTextReader(reader); ! 70: if (ret != 0) { ! 71: fprintf(stderr, "%s : failed to parse\n", filename); ! 72: } ! 73: } else { ! 74: fprintf(stderr, "Unable to open %s\n", filename); ! 75: } ! 76: } ! 77: ! 78: int main(int argc, char **argv) { ! 79: if (argc != 2) ! 80: return(1); ! 81: ! 82: /* ! 83: * this initialize the library and check potential ABI mismatches ! 84: * between the version it was compiled for and the actual shared ! 85: * library used. ! 86: */ ! 87: LIBXML_TEST_VERSION ! 88: ! 89: streamFile(argv[1]); ! 90: ! 91: /* ! 92: * Cleanup function for the XML library. ! 93: */ ! 94: xmlCleanupParser(); ! 95: /* ! 96: * this is to debug memory for regression tests ! 97: */ ! 98: xmlMemoryDump(); ! 99: return(0); ! 100: } ! 101: ! 102: #else ! 103: int main(void) { ! 104: fprintf(stderr, "XInclude support not compiled in\n"); ! 105: exit(1); ! 106: } ! 107: #endif