9304e8b0dcc5f0e4dac0de070ade6ae058318e7c
[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->type == XML_ELEMENT_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_string(xmlChar *s)
84 {
85   for (char *p = (char *) s; *p; p++)
86     if (*p == '\n')
87       printf ("\\n");
88     else
89       putchar (*p);
90 }
91
92 static void
93 print_cdata (xmlNode * a_node)
94 {
95   for (xmlNode *node = a_node; node; node = node->next)
96     {
97       if (node->type == XML_CDATA_SECTION_NODE)
98         {
99           print_string (node->content);
100           putchar ('\n');
101         }
102
103       print_cdata (node->children);
104     }
105 }
106
107 static void
108 print_attribute (xmlNode *node, const char *attr)
109 {
110   for (; node; node = node->next)
111     {
112       const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
113       if (s)
114         printf ("%s %s\n", node->name, s);
115
116       print_attribute (node->children, attr);
117     }
118 }
119
120 static void
121 print_text (xmlNode *node)
122 {
123   for (; node; node = node->next)
124     {
125       if (node->type == XML_ELEMENT_NODE)
126         {
127           printf ("%s", node->name);
128           for (xmlNode *child = node->children; child; child = child->next)
129             if (child->type == XML_TEXT_NODE)
130               {
131                 putchar (' ');
132                 print_string (child->content);
133               }
134           putchar ('\n');
135         }
136
137       print_text (node->children);
138     }
139 }
140
141 static void
142 usage (void)
143 {
144   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
145   exit (1);
146 }
147
148 int
149 main (int argc, char **argv)
150 {
151   if (argc != 3)
152     usage ();
153
154   LIBXML_TEST_VERSION;
155
156   xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
157   if (doc == NULL)
158     {
159       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
160       exit (1);
161     }
162
163   xmlNode *root = xmlDocGetRootElement(doc);
164
165   if (!strcmp(argv[2], "containment"))
166     print_containment (root);
167   else if (!strcmp(argv[2], "attributes"))
168     print_attributes (root);
169   else if (!strcmp(argv[2], "cdata"))
170     print_cdata (root);
171   else if (!strcmp(argv[2], "text"))
172     print_text (root);
173   else if (!strncmp(argv[2], "attr:", 5))
174     print_attribute (root, argv[2] + 5);
175   else if (!strcmp(argv[2], "labels"))
176     print_labels (root);
177   else
178     usage ();
179
180   xmlFreeDoc(doc);
181   xmlCleanupParser();
182
183   return 0;
184 }