Document some elements.
[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 *node)
8 {
9   for (; node; node = node->next)
10     {
11       if (node->type == XML_ELEMENT_NODE)
12         {
13           printf ("%s", node->name);
14           for (xmlNode *child = node->children; child; child = child->next)
15             {
16               putchar (' ');
17               if (child->type == XML_ELEMENT_NODE)
18                 {
19                   printf ("%s", child->name);
20
21                   int n = 0;
22                   while (child->next
23                          && child->next->type == XML_ELEMENT_NODE
24                          && !strcmp((char *) child->name, (char *) child->next->name))
25                     {
26                       child = child->next;
27                       n++;
28                     }
29                   if (n > 0)
30                     putchar ('+');
31                 }
32               else if (child->type == XML_TEXT_NODE)
33                 printf ("<text>");
34               else if (child->type == XML_CDATA_SECTION_NODE)
35                 printf ("<cdata>");
36               else
37                 printf ("<%d>", child->type);
38             }
39           putchar ('\n');
40         }
41
42       print_containment (node->children);
43     }
44 }
45
46 static void
47 print_labels (xmlNode *node)
48 {
49   for (; node; node = node->next)
50     {
51       if (node->type == XML_ELEMENT_NODE
52           && !strcmp((char *) node->name, "label")
53           && node->parent->type == XML_ELEMENT_NODE
54           && !strcmp((char *) node->parent->name, "container"))
55         {
56           for (xmlNode *child = node->children; child; child = child->next)
57             if (child->type == XML_TEXT_NODE)
58               puts ((char *) child->content);
59         }
60
61       print_labels (node->children);
62     }
63 }
64
65 static void
66 print_attributes (xmlNode * a_node)
67 {
68   for (xmlNode *node = a_node; node; node = node->next)
69     {
70       if (node->properties)
71         {
72           printf ("%s", node->name);
73           for (xmlAttr *attr = node->properties; attr; attr = attr->next)
74             printf (" %s", attr->name);
75           putchar ('\n');
76         }
77
78       print_attributes (node->children);
79     }
80 }
81
82 static void
83 usage (void)
84 {
85   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
86   exit (1);
87 }
88
89 int
90 main (int argc, char **argv)
91 {
92   if (argc != 3)
93     usage ();
94
95   LIBXML_TEST_VERSION;
96
97   xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
98   if (doc == NULL)
99     {
100       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
101       exit (1);
102     }
103
104   xmlNode *root = xmlDocGetRootElement(doc);
105
106   if (!strcmp(argv[2], "containment"))
107     print_containment (root);
108   else if (!strcmp(argv[2], "attributes"))
109     print_attributes (root);
110   else if (!strcmp(argv[2], "labels"))
111     print_labels (root);
112   else
113     usage ();
114
115   xmlFreeDoc(doc);
116   xmlCleanupParser();
117
118   return 0;
119 }