Work on structure XML description.
[pspp] / parse-xml.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <libxml/parser.h>
4 #include <libxml/tree.h>
5
6 static void
7 print_containment (xmlNode * a_node)
8 {
9   for (xmlNode *node = a_node; node; node = node->next)
10     {
11       const xmlNode *parent = node->parent;
12       if (parent->type == XML_ELEMENT_NODE)
13         printf ("%s ", parent->name);
14       else
15         printf ("<root> ");
16
17       if (node->type == XML_ELEMENT_NODE)
18         printf ("%s", node->name);
19       else if (node->type == XML_TEXT_NODE)
20         printf("<text>");
21       else if (node->type == XML_CDATA_SECTION_NODE)
22         printf("<cdata>");
23       else
24         printf("<other>");
25
26       putchar('\n');
27
28       print_containment (node->children);
29     }
30 }
31
32 static void
33 print_attributes (xmlNode * a_node)
34 {
35   for (xmlNode *node = a_node; node; node = node->next)
36     {
37       for (xmlAttr *attr = node->properties; attr; attr = attr->next)
38         printf ("%s %s\n", node->name, attr->name);
39
40       print_attributes (node->children);
41     }
42 }
43
44 static void
45 usage (void)
46 {
47   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
48   exit (1);
49 }
50
51 int
52 main (int argc, char **argv)
53 {
54   if (argc != 3)
55     usage ();
56
57   LIBXML_TEST_VERSION;
58
59   xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
60   if (doc == NULL)
61     {
62       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
63       exit (1);
64     }
65
66   xmlNode *root = xmlDocGetRootElement(doc);
67
68   if (!strcmp(argv[2], "containment"))
69     print_containment (root);
70   else if (!strcmp(argv[2], "attributes"))
71     print_attributes (root);
72   else
73     usage ();
74
75   xmlFreeDoc(doc);
76   xmlCleanupParser();
77
78   return 0;
79 }