c0445052ed017f6ea80111568dc581884c69d4e1
[pspp] / parse-xml.c
1 #include <stdio.h>
2 #include <libxml/parser.h>
3 #include <libxml/tree.h>
4
5 static void
6 print_containment (xmlNode * a_node)
7 {
8   for (xmlNode *node = a_node; node; node = node->next)
9     {
10       if (node->type == XML_ELEMENT_NODE)
11         {
12           const xmlNode *parent = node->parent;
13           if (parent->type == XML_ELEMENT_NODE)
14             printf ("%s %s\n", parent->name, node->name);
15           else if (parent->type == XML_DOCUMENT_NODE)
16             printf ("<root> %s\n", node->name);
17         }
18
19       print_containment (node->children);
20     }
21 }
22
23 int
24 main (int argc, char **argv)
25 {
26   if (argc != 2)
27     {
28       fprintf (stderr, "usage: %s FILE.xml\n", argv[0]);
29       exit (1);
30     }
31
32   LIBXML_TEST_VERSION;
33
34   xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
35   if (doc == NULL)
36     {
37       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
38       exit (1);
39     }
40
41   xmlNode *root = xmlDocGetRootElement(doc);
42   print_containment(root);
43   xmlFreeDoc(doc);
44   xmlCleanupParser();
45
46   return 0;
47 }