summaryrefslogtreecommitdiff
path: root/misc/samples/xml_sax.c
diff options
context:
space:
mode:
authorHolger Dengler <dengler@linutronix.de>2012-10-22 17:05:03 +0200
committerHolger Dengler <dengler@linutronix.de>2012-10-22 17:09:38 +0200
commit494b16d6fbd565225e4f9ddaf2b813b520271753 (patch)
treef4fd715a9a99e0b3303cca7b6cb0ac19588a36ac /misc/samples/xml_sax.c
parent73b8bf14798e661351a8e51237cac38bdc824d86 (diff)
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 <dengler@linutronix.de>
Diffstat (limited to 'misc/samples/xml_sax.c')
-rw-r--r--misc/samples/xml_sax.c109
1 files changed, 109 insertions, 0 deletions
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 <string.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+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<len) && (i < 30);i++)
+ output[i] = ch[i];
+ output[i] = 0;
+
+ fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
+}
+
+static void
+cb_comment(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
+{
+ cb_total++;
+ fprintf(stdout, "SAX.comment(%s)\n", value);
+}
+
+/*
+ * Define callback struct
+ */
+xmlSAXHandler debugSAXHandlerStruct = {
+ NULL, NULL, NULL, NULL, NULL, NULL,
+ NULL, NULL, NULL, NULL, NULL, NULL,
+ cb_start_document,
+ cb_end_document,
+ cb_start_element,
+ cb_end_element,
+ NULL,
+ cb_characters,
+ NULL, NULL,
+ cb_comment,
+ NULL, NULL, NULL, NULL, NULL, NULL,
+ 1
+};
+xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
+
+int main(int argc, char **argv) {
+ int rc;
+
+ if (argc != 2)
+ return 1;
+
+ cb_total = 0;
+ rc = xmlSAXUserParseFile(debugSAXHandler, NULL, argv[1]);
+ if (rc)
+ fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", rc);
+
+ fprintf(stdout, "\ncallback calls: %d\n", cb_total);
+
+ xmlCleanupParser();
+ xmlMemoryDump();
+
+ return(0);
+}