Document all the elements, plus the attributes of heading.
[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 && node->parent->type != XML_DOCUMENT_NODE)
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 print_attribute (xmlNode *node, const char *attr)
84 {
85   for (; node; node = node->next)
86     {
87       const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
88       if (s)
89         puts (s);
90
91       print_attribute (node->children, attr);
92     }
93 }
94
95 static void
96 usage (void)
97 {
98   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
99   exit (1);
100 }
101
102 int
103 main (int argc, char **argv)
104 {
105   if (argc != 3)
106     usage ();
107
108   LIBXML_TEST_VERSION;
109
110   xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
111   if (doc == NULL)
112     {
113       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
114       exit (1);
115     }
116
117   xmlNode *root = xmlDocGetRootElement(doc);
118
119   if (!strcmp(argv[2], "containment"))
120     print_containment (root);
121   else if (!strcmp(argv[2], "attributes"))
122     print_attributes (root);
123   else if (!strncmp(argv[2], "attr:", 5))
124     print_attribute (root, argv[2] + 5);
125   else if (!strcmp(argv[2], "labels"))
126     print_labels (root);
127   else
128     usage ();
129
130   xmlFreeDoc(doc);
131   xmlCleanupParser();
132
133   return 0;
134 }