|
Packit Service |
a31ea6 |
/**
|
|
Packit Service |
a31ea6 |
* section: xmlReader
|
|
Packit Service |
a31ea6 |
* synopsis: Show how to extract subdocuments with xmlReader
|
|
Packit Service |
a31ea6 |
* purpose: Demonstrate the use of xmlTextReaderPreservePattern()
|
|
Packit Service |
a31ea6 |
* to parse an XML file with the xmlReader while collecting
|
|
Packit Service |
a31ea6 |
* only some subparts of the document.
|
|
Packit Service |
a31ea6 |
* (Note that the XMLReader functions require libxml2 version later
|
|
Packit Service |
a31ea6 |
* than 2.6.)
|
|
Packit Service |
a31ea6 |
* usage: reader3
|
|
Packit Service |
a31ea6 |
* test: reader3 > reader3.tmp && diff reader3.tmp $(srcdir)/reader3.res
|
|
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/xmlreader.h>
|
|
Packit Service |
a31ea6 |
|
|
Packit Service |
a31ea6 |
#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_PATTERN_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
|
|
Packit Service |
a31ea6 |
|
|
Packit Service |
a31ea6 |
|
|
Packit Service |
a31ea6 |
/**
|
|
Packit Service |
a31ea6 |
* streamFile:
|
|
Packit Service |
a31ea6 |
* @filename: the file name to parse
|
|
Packit Service |
a31ea6 |
*
|
|
Packit Service |
a31ea6 |
* Parse and print information about an XML file.
|
|
Packit Service |
a31ea6 |
*
|
|
Packit Service |
a31ea6 |
* Returns the resulting doc with just the elements preserved.
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
static xmlDocPtr
|
|
Packit Service |
a31ea6 |
extractFile(const char *filename, const xmlChar *pattern) {
|
|
Packit Service |
a31ea6 |
xmlDocPtr doc;
|
|
Packit Service |
a31ea6 |
xmlTextReaderPtr reader;
|
|
Packit Service |
a31ea6 |
int ret;
|
|
Packit Service |
a31ea6 |
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* build an xmlReader for that file
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
reader = xmlReaderForFile(filename, NULL, 0);
|
|
Packit Service |
a31ea6 |
if (reader != NULL) {
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* add the pattern to preserve
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
if (xmlTextReaderPreservePattern(reader, pattern, NULL) < 0) {
|
|
Packit Service |
a31ea6 |
fprintf(stderr, "%s : failed add preserve pattern %s\n",
|
|
Packit Service |
a31ea6 |
filename, (const char *) pattern);
|
|
Packit Service |
a31ea6 |
}
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* Parse and traverse the tree, collecting the nodes in the process
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
ret = xmlTextReaderRead(reader);
|
|
Packit Service |
a31ea6 |
while (ret == 1) {
|
|
Packit Service |
a31ea6 |
ret = xmlTextReaderRead(reader);
|
|
Packit Service |
a31ea6 |
}
|
|
Packit Service |
a31ea6 |
if (ret != 0) {
|
|
Packit Service |
a31ea6 |
fprintf(stderr, "%s : failed to parse\n", filename);
|
|
Packit Service |
a31ea6 |
xmlFreeTextReader(reader);
|
|
Packit Service |
a31ea6 |
return(NULL);
|
|
Packit Service |
a31ea6 |
}
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* get the resulting nodes
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
doc = xmlTextReaderCurrentDoc(reader);
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* Free up the reader
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
xmlFreeTextReader(reader);
|
|
Packit Service |
a31ea6 |
} else {
|
|
Packit Service |
a31ea6 |
fprintf(stderr, "Unable to open %s\n", filename);
|
|
Packit Service |
a31ea6 |
return(NULL);
|
|
Packit Service |
a31ea6 |
}
|
|
Packit Service |
a31ea6 |
return(doc);
|
|
Packit Service |
a31ea6 |
}
|
|
Packit Service |
a31ea6 |
|
|
Packit Service |
a31ea6 |
int main(int argc, char **argv) {
|
|
Packit Service |
a31ea6 |
const char *filename = "test3.xml";
|
|
Packit Service |
a31ea6 |
const char *pattern = "preserved";
|
|
Packit Service |
a31ea6 |
xmlDocPtr doc;
|
|
Packit Service |
a31ea6 |
|
|
Packit Service |
a31ea6 |
if (argc == 3) {
|
|
Packit Service |
a31ea6 |
filename = argv[1];
|
|
Packit Service |
a31ea6 |
pattern = argv[2];
|
|
Packit Service |
a31ea6 |
}
|
|
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 |
doc = extractFile(filename, (const xmlChar *) pattern);
|
|
Packit Service |
a31ea6 |
if (doc != NULL) {
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* ouptut the result.
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
xmlDocDump(stdout, doc);
|
|
Packit Service |
a31ea6 |
/*
|
|
Packit Service |
a31ea6 |
* don't forget to free up the doc
|
|
Packit Service |
a31ea6 |
*/
|
|
Packit Service |
a31ea6 |
xmlFreeDoc(doc);
|
|
Packit Service |
a31ea6 |
}
|
|
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 |
|
|
Packit Service |
a31ea6 |
#else
|
|
Packit Service |
a31ea6 |
int main(void) {
|
|
Packit Service |
a31ea6 |
fprintf(stderr, "Reader, Pattern or output support not compiled in\n");
|
|
Packit Service |
a31ea6 |
exit(1);
|
|
Packit Service |
a31ea6 |
}
|
|
Packit Service |
a31ea6 |
#endif
|