#include #include #include /* Compile with: gcc --std=c99 -o walk walk.c `pkg-config --libs --cflags libxml-2.0` */ static xmlDoc* my_parse_document (const xmlChar *filename) { xmlParserCtxt *ctxt = NULL; xmlDoc *document = NULL; ctxt = xmlCreateFileParserCtxt(filename); xmlCtxtUseOptions(ctxt, XML_PARSE_COMPACT | XML_PARSE_RECOVER | XML_PARSE_NOERROR); if (xmlParseDocument(ctxt) == 0) { document = ctxt->myDoc; ctxt->myDoc = NULL; } xmlFreeParserCtxt(ctxt); return document; } /* Walk the given DOM and show more interrest on */ void walk (xmlNode *node, int depth) { if (node->type == XML_ELEMENT_NODE) { int i = 0; for (; i < depth; ++i) { printf(" "); } printf("%s ", node->name); if (xmlStrcmp(node->name, "scalar") == 0) { xmlChar *value = xmlGetNoNsProp(node, "parName"); printf(" parName = %s", value); if (value) xmlFree(value); } printf("\n"); } else if (node->type == XML_DOCUMENT_NODE) { printf("/\n"); } xmlNode *child = node; for (child = node->children; child; child = child->next) { walk(child, depth + 1); } } int main (int argc, char **argv) { xmlDoc *document = NULL; xmlNode *root = NULL; xmlNode *node = NULL; const xmlChar *filename = NULL; if (argc == 1) { printf("Usage: filename\n"); return 1; } filename = (const xmlChar *) argv[1]; document = my_parse_document(filename); if (document == NULL) { printf("Failed to parse %s\n", filename); return 1; } walk((xmlNode *) document, 0); xmlFreeDoc(document); xmlCleanupParser(); return 0; }