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/xml_dom.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 misc/samples/xml_dom.c (limited to 'misc/samples/xml_dom.c') 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); + } +} -- cgit v1.2.3