Write a function in C that recieves an XML file and traverse

Write a function in C that recieves an XML file and traverses the tree in the following function with recursion and prints the contents. (i believe in pre-order)

void display_xmltree(xmlDocPtr doc)
{
   xmlNode *root_node = xmlDocGetRootElement(doc);
  

//recursion code here
   // USE NAVIGATION FROM libxml2 API
   // http://xmlsoft.org/html/libxml-tree.html
}

Solution

#include #include \"xmlparse.h\" void startElement (void *userData, const char *name, const char **atts) { int i; int *depthPtr = userData; for (i = 0; i < *depthPtr; i++) putchar(\'\\t\'); puts(\"I found the element:\"); puts(name); *depthPtr += 1; } void endElement(void *userData, const char *name) { int *depthPtr = userData; *depthPtr -= 1; } int main() { char buf[BUFSIZ]; XML_Parser parser = XML_ParserCreate(NULL); int done; int depth = 0; XML_SetUserData(parser, &depth); XML_SetElementHandler(parser, startElement, endElement); do { size_t len = fread(buf, 1, sizeof(buf), stdin); done = len < sizeof(buf); if (!XML_Parse(parser, buf, len, done)) { fprintf(stderr, \"%s at line %d\ \", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); return 1; } } while (!done); XML_ParserFree(parser); return 0; }
Write a function in C that recieves an XML file and traverses the tree in the following function with recursion and prints the contents. (i believe in pre-order

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site