123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include <stdio.h>
- #include <libxml/xmlreader.h>
- #if defined(LIBXML_READER_ENABLED) && defined(LIBXML_PATTERN_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
- static xmlDocPtr
- extractFile(const char *filename, const xmlChar *pattern) {
- xmlDocPtr doc;
- xmlTextReaderPtr reader;
- int ret;
-
- reader = xmlReaderForFile(filename, NULL, 0);
- if (reader != NULL) {
-
- if (xmlTextReaderPreservePattern(reader, pattern, NULL) < 0) {
- fprintf(stderr, "%s : failed add preserve pattern %s\n",
- filename, (const char *) pattern);
- }
-
- ret = xmlTextReaderRead(reader);
- while (ret == 1) {
- ret = xmlTextReaderRead(reader);
- }
- if (ret != 0) {
- fprintf(stderr, "%s : failed to parse\n", filename);
- xmlFreeTextReader(reader);
- return(NULL);
- }
-
- doc = xmlTextReaderCurrentDoc(reader);
-
- xmlFreeTextReader(reader);
- } else {
- fprintf(stderr, "Unable to open %s\n", filename);
- return(NULL);
- }
- return(doc);
- }
- int main(int argc, char **argv) {
- const char *filename = "test3.xml";
- const char *pattern = "preserved";
- xmlDocPtr doc;
- if (argc == 3) {
- filename = argv[1];
- pattern = argv[2];
- }
-
- LIBXML_TEST_VERSION
- doc = extractFile(filename, (const xmlChar *) pattern);
- if (doc != NULL) {
-
- xmlDocDump(stdout, doc);
-
- xmlFreeDoc(doc);
- }
-
- xmlCleanupParser();
-
- xmlMemoryDump();
- return(0);
- }
- #else
- int main(void) {
- fprintf(stderr, "Reader, Pattern or output support not compiled in\n");
- exit(1);
- }
- #endif
|