3 #include <libxml/parser.h>
4 #include <libxml/tree.h>
7 print_parents(xmlNode *node)
9 for (; node; node = node->next)
11 if (node->type == XML_ELEMENT_NODE)
13 for (xmlNode *child = node->children; child; child = child->next)
15 if (child->type == XML_ELEMENT_NODE)
16 printf ("%s %s\n", node->name, child->name);
17 else if (child->type == XML_TEXT_NODE)
18 printf ("%s <text>\n", node->name);
19 else if (child->type == XML_CDATA_SECTION_NODE)
20 printf ("%s <cdata>\n", node->name);
21 else if (child->type == XML_COMMENT_NODE)
22 printf ("%s <comment>\n", node->name);
24 printf ("%s <%d>\n", node->name, child->type);
28 print_parents (node->children);
33 print_containment (xmlNode *node)
35 for (; node; node = node->next)
37 if (node->type == XML_ELEMENT_NODE)
39 const char *child_names[5000];
40 int child_name_cnt[5000];
42 for (xmlNode *child = node->children; child; child = child->next)
46 if (child->type == XML_ELEMENT_NODE)
48 name = (char *) child->name;
49 if (!strcmp((char *) node->name, "derivedVariable")
50 && !strcmp((char *) name, "extension"))
54 (char *) xmlGetProp (child, (xmlChar *) "from"),
55 (char *) xmlGetProp (child, (xmlChar *) "helpId"));
59 else if (child->type == XML_TEXT_NODE)
61 else if (child->type == XML_CDATA_SECTION_NODE)
63 else if (child->type == XML_COMMENT_NODE)
66 //printf ("comment %s\n", (char *) child->content);
73 for (int i = 0; i < n_names; i++)
74 if (!strcmp(name, child_names[i]))
80 child_names[n_names] = name;
81 child_name_cnt[n_names] = 1;
87 printf ("%s", node->name);
88 for (int i = 0; i < n_names; i++)
90 printf (" %s", child_names[i]);
91 if (child_name_cnt[i] > 1)
97 print_containment (node->children);
102 print_labels (xmlNode *node)
104 for (; node; node = node->next)
106 if (node->type == XML_ELEMENT_NODE
107 && !strcmp((char *) node->name, "label")
108 && node->parent->type == XML_ELEMENT_NODE
109 && !strcmp((char *) node->parent->name, "container"))
111 for (xmlNode *child = node->children; child; child = child->next)
112 if (child->type == XML_TEXT_NODE)
113 puts ((char *) child->content);
116 print_labels (node->children);
121 compare_strings(const void *a_, const void *b_)
123 const void *const *ap = a_;
124 const void *const *bp = b_;
125 return strcmp (*ap, *bp);
129 print_attributes (xmlNode * a_node)
131 for (xmlNode *node = a_node; node; node = node->next)
133 if (node->type == XML_ELEMENT_NODE)
135 printf ("%s<-%s", node->name, node->parent->name);
139 for (xmlAttr *attr = node->properties; attr; attr = attr->next)
140 if (!strcmp((char *) attr->name, "baseFormat"))
141 printf(" baseFormat=%s", attr->children->content);
143 attrs[n_attrs++] = (char *) attr->name;
144 qsort(attrs, n_attrs, sizeof *attrs, compare_strings);
146 for (int i = 0; i < n_attrs; i++)
147 printf (" %s", attrs[i]);
151 print_attributes (node->children);
156 print_string(xmlChar *s)
158 for (char *p = (char *) s; *p; p++)
166 print_cdata (xmlNode * a_node)
168 for (xmlNode *node = a_node; node; node = node->next)
170 if (node->type == XML_CDATA_SECTION_NODE)
172 print_string (node->content);
176 print_cdata (node->children);
181 print_attribute (xmlNode *node, const char *attr)
183 for (; node; node = node->next)
185 const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
187 printf ("%s %s=%s\n", node->name, attr, s);
189 print_attribute (node->children, attr);
194 print_element (xmlDoc *doc, xmlNode *node, const char *element)
196 for (; node; node = node->next)
198 if (!strcmp(element, (char *) node->name))
200 xmlBuffer *buf = xmlBufferCreate();
201 xmlNodeDump (buf, doc, node, 0, 1);
202 xmlBufferDump (stdout, buf);
207 print_element (doc, node->children, element);
211 static __attribute__((unused)) xmlNode *
212 find_page_setup (xmlNode *node)
214 for (; node; node = node->next)
216 if (node->name && !strcmp ((char *) node->name, "pageSetup"))
219 xmlNode *ps = find_page_setup (node->children);
227 print_text (xmlNode *node)
229 for (; node; node = node->next)
231 if (node->type == XML_ELEMENT_NODE)
233 printf ("%s", node->name);
234 for (xmlNode *child = node->children; child; child = child->next)
235 if (child->type == XML_TEXT_NODE)
238 print_string (child->content);
243 print_text (node->children);
250 fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
255 main (int argc, char **argv)
262 xmlDoc *doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS);
265 fprintf (stderr, "error: could not parse file %s\n", argv[1]);
269 xmlNode *root = xmlDocGetRootElement(doc);
272 /* Limit what we look at to pageSetup node and below. */
273 root = find_page_setup(root);
279 if (!strcmp(argv[2], "parents"))
280 print_parents (root);
281 else if (!strcmp(argv[2], "containment"))
282 print_containment (root);
283 else if (!strcmp(argv[2], "attributes"))
284 print_attributes (root);
285 else if (!strcmp(argv[2], "cdata"))
287 else if (!strcmp(argv[2], "text"))
289 else if (!strncmp(argv[2], "element:", 8))
290 print_element (doc, root, argv[2] + 8);
291 else if (!strncmp(argv[2], "attr:", 5))
292 print_attribute (root, argv[2] + 5);
293 else if (!strcmp(argv[2], "labels"))