From 494b16d6fbd565225e4f9ddaf2b813b520271753 Mon Sep 17 00:00:00 2001 From: Holger Dengler Date: Mon, 22 Oct 2012 17:05:03 +0200 Subject: Misc/xml: Add new XML fasttrack Add new miscellaneous section with an XML fasttrack. Coding samples are located in misc/samples/. Signed-off-by: Holger Dengler --- misc/samples/.gitignore | 2 + misc/samples/Makefile | 10 +++++ misc/samples/xml_dom.c | 55 +++++++++++++++++++++++ misc/samples/xml_full.xml | 24 ++++++++++ misc/samples/xml_sax.c | 109 +++++++++++++++++++++++++++++++++++++++++++++ misc/samples/xml_small.xml | 14 ++++++ 6 files changed, 214 insertions(+) create mode 100644 misc/samples/.gitignore create mode 100644 misc/samples/Makefile create mode 100644 misc/samples/xml_dom.c create mode 100644 misc/samples/xml_full.xml create mode 100644 misc/samples/xml_sax.c create mode 100644 misc/samples/xml_small.xml (limited to 'misc/samples') diff --git a/misc/samples/.gitignore b/misc/samples/.gitignore new file mode 100644 index 0000000..805c89c --- /dev/null +++ b/misc/samples/.gitignore @@ -0,0 +1,2 @@ +xml_dom +xml_sax diff --git a/misc/samples/Makefile b/misc/samples/Makefile new file mode 100644 index 0000000..cd411f2 --- /dev/null +++ b/misc/samples/Makefile @@ -0,0 +1,10 @@ +XML_TARGETS=xml_dom xml_sax +XML_CFLAGS=$(shell xml2-config --cflags) + +all: $(XML_TARGETS) + +%: %.c + $(CC) $(XML_CFLAGS) -g -O0 -l xml2 -o $@ $^ + +clean: + rm -r $(XML_TARGETS) diff --git a/misc/samples/xml_dom.c b/misc/samples/xml_dom.c new file mode 100644 index 0000000..66166dd --- /dev/null +++ b/misc/samples/xml_dom.c @@ -0,0 +1,55 @@ +#include +#include +#include + +static void print_element_names(xmlNode *); + +int main(int argc, char **argv) { + xmlDocPtr my_doc = NULL; + xmlNode *root_element = NULL; + char *filename; + int rc = 1; + + if (argc != 2) { + rc = 1; + goto out; + } + filename = argv[1]; + + /* ABI checking (opt.) */ + LIBXML_TEST_VERSION + + my_doc = xmlReadFile(filename, NULL, 0); + if (!my_doc) { + rc = 1; + goto out_cleanup; + } + + /*Get the root element node */ + root_element = xmlDocGetRootElement(my_doc); + + print_element_names(root_element); + + rc = 0; +out_freedoc: + /* Free parsed DOM tree */ + xmlFreeDoc(my_doc); +out_cleanup: + /* Cleanup function for the XML library */ + xmlCleanupParser(); +out: + return(rc); +} + +static void print_element_names(xmlNode * a_node) +{ + xmlNode *cur_node = NULL; + + for (cur_node = a_node; cur_node; cur_node = cur_node->next) { + if (cur_node->type == XML_ELEMENT_NODE) { + printf("node type: Element, name: %s\n", cur_node->name); + } + + print_element_names(cur_node->children); + } +} diff --git a/misc/samples/xml_full.xml b/misc/samples/xml_full.xml new file mode 100644 index 0000000..c8b0ecd --- /dev/null +++ b/misc/samples/xml_full.xml @@ -0,0 +1,24 @@ + + + + +The Lord Of The Rings +J.R.R. Tolkien + + +Maitreyi +Mircea Eliade + + +The Wall +Pink Floyd +Another Brick in the Wall +Mother + + +Come on Over +Shania Twain +From This Moment On +You're Still The One + + diff --git a/misc/samples/xml_sax.c b/misc/samples/xml_sax.c new file mode 100644 index 0000000..b586a21 --- /dev/null +++ b/misc/samples/xml_sax.c @@ -0,0 +1,109 @@ +/* + * Simple SAX example + */ + +#include +#include +#include + +static int cb_total = 0; + +/* + * Callback implementations + */ +static void +cb_start_document(void *ctx ATTRIBUTE_UNUSED) +{ + cb_total++; + fprintf(stdout, "SAX.startDocument()\n"); +} + +static void +cb_end_document(void *ctx ATTRIBUTE_UNUSED) +{ + cb_total++; + fprintf(stdout, "SAX.endDocument()\n"); +} + +static void +cb_start_element(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts) +{ + int i; + + cb_total++; + fprintf(stdout, "SAX.startElement(%s", (char *) name); + if (atts != NULL) { + for (i = 0;(atts[i] != NULL);i++) { + fprintf(stdout, ", %s='", atts[i++]); + if (atts[i] != NULL) + fprintf(stdout, "%s'", atts[i]); + } + } + fprintf(stdout, ")\n"); +} + +static void +cb_end_element(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name) +{ + cb_total++; + fprintf(stdout, "SAX.endElement(%s)\n", (char *) name); +} + +static void +cb_characters(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len) +{ + char output[40]; + int i; + + cb_total++; + for (i = 0;(i + + + +The Lord Of The Rings +J.R.R. Tolkien + + +The Wall +Pink Floyd +Another Brick in the Wall +Mother + + -- cgit v1.2.3