aaronvan

Untitled

Nov 23rd, 2020
652
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libxml/parser.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6.  
  7.     const char *filename = argv[1];
  8.     xmlDocPtr doc;
  9.     xmlNodePtr root, node;
  10.  
  11.     // return "file handle" to read the file
  12.     doc = xmlParseFile(filename);
  13.    
  14.     if (doc == NULL) {
  15.         fprintf(stderr, "Unable to parse data\n");
  16.         xmlFreeDoc(doc);
  17.         exit (1);
  18.     } else {
  19.         puts("Data parsed successfully");
  20.     }
  21.  
  22.     // return pointer to the root element
  23.     root = xmlDocGetRootElement(doc);
  24.    
  25.     if (root == NULL) {
  26.         fprintf(stderr, "Can't read data\n");
  27.         xmlFreeDoc(doc);
  28.         exit (1);
  29.     } else {
  30.         printf("Root node is '%s'\n", root->name);
  31.     }
  32.        
  33.     if (root->children) {
  34.         printf("Child nodes:\n");
  35.         for (node = root->children; node; node = node->next) {
  36.             if (node->type == XML_ELEMENT_NODE) {
  37.                 printf("\t%s\n", node->name);
  38.             }
  39.         }
  40.     }
  41.     xmlFreeDoc(doc);
  42.  
  43.     return EXIT_SUCCESS;
  44. }
  45.  
RAW Paste Data