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[512];
40 int child_name_cnt[512];
42 for (xmlNode *child = node->children; child; child = child->next)
46 if (child->type == XML_ELEMENT_NODE)
47 name = (char *) child->name;
48 else if (child->type == XML_TEXT_NODE)
50 else if (child->type == XML_CDATA_SECTION_NODE)
52 else if (child->type == XML_COMMENT_NODE)
55 //printf ("comment %s\n", (char *) child->content);
61 for (int i = 0; i < n_names; i++)
62 if (!strcmp(name, child_names[i]))
67 child_names[n_names] = name;
68 child_name_cnt[n_names] = 1;
74 printf ("%s", node->name);
75 for (int i = 0; i < n_names; i++)
77 printf (" %s", child_names[i]);
78 if (child_name_cnt[i] > 1)
84 print_containment (node->children);
89 print_labels (xmlNode *node)
91 for (; node; node = node->next)
93 if (node->type == XML_ELEMENT_NODE
94 && !strcmp((char *) node->name, "label")
95 && node->parent->type == XML_ELEMENT_NODE
96 && !strcmp((char *) node->parent->name, "container"))
98 for (xmlNode *child = node->children; child; child = child->next)
99 if (child->type == XML_TEXT_NODE)
100 puts ((char *) child->content);
103 print_labels (node->children);
108 print_attributes (xmlNode * a_node)
110 for (xmlNode *node = a_node; node; node = node->next)
112 if (node->type == XML_ELEMENT_NODE)
114 printf ("%s", node->name);
115 for (xmlAttr *attr = node->properties; attr; attr = attr->next)
116 printf (" %s", attr->name);
120 print_attributes (node->children);
125 print_string(xmlChar *s)
127 for (char *p = (char *) s; *p; p++)
135 print_cdata (xmlNode * a_node)
137 for (xmlNode *node = a_node; node; node = node->next)
139 if (node->type == XML_CDATA_SECTION_NODE)
141 print_string (node->content);
145 print_cdata (node->children);
150 print_attribute (xmlNode *node, const char *attr)
152 for (; node; node = node->next)
154 const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
156 printf ("%s %s=%s\n", node->name, attr, s);
158 print_attribute (node->children, attr);
163 print_xml (xmlDoc *doc, xmlNode *node)
165 xmlBuffer *buf = xmlBufferCreate();
166 xmlNodeDump (buf, doc, node, 0, 1);
167 xmlBufferDump (stdout, buf);
173 print_element (xmlDoc *doc, xmlNode *node, const char *element)
175 for (; node; node = node->next)
177 if (node->name && !strcmp(element, (char *) node->name))
178 print_xml (doc, node);
180 print_element (doc, node->children, element);
185 print_id (xmlDoc *doc, xmlNode *node, const char *id)
187 for (; node; node = node->next)
189 if (node->type == XML_ELEMENT_NODE)
191 const char *node_id = (char *) xmlGetProp (node, (xmlChar *) "id");
192 if (node_id && !strcmp (node_id, id))
194 print_xml (doc, node);
199 print_id (doc, node->children, id);
203 static __attribute__((unused)) xmlNode *
204 find_page_setup (xmlNode *node)
206 for (; node; node = node->next)
208 if (node->name && !strcmp ((char *) node->name, "pageSetup"))
211 xmlNode *ps = find_page_setup (node->children);
219 print_text (xmlNode *node)
221 for (; node; node = node->next)
223 if (node->type == XML_ELEMENT_NODE)
225 printf ("%s", node->name);
226 for (xmlNode *child = node->children; child; child = child->next)
227 if (child->type == XML_TEXT_NODE)
230 print_string (child->content);
235 print_text (node->children);
242 fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
247 main (int argc, char **argv)
254 xmlDoc *doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS);
257 fprintf (stderr, "error: could not parse file %s\n", argv[1]);
261 xmlNode *root = xmlDocGetRootElement(doc);
264 /* Limit what we look at to pageSetup node and below. */
265 root = find_page_setup(root);
271 if (!strcmp(argv[2], "parents"))
272 print_parents (root);
273 else if (!strcmp(argv[2], "containment"))
274 print_containment (root);
275 else if (!strcmp(argv[2], "attributes"))
276 print_attributes (root);
277 else if (!strcmp(argv[2], "cdata"))
279 else if (!strcmp(argv[2], "text"))
281 else if (!strncmp(argv[2], "element:", 8))
282 print_element (doc, root, argv[2] + 8);
283 else if (!strncmp(argv[2], "attr:", 5))
284 print_attribute (root, argv[2] + 5);
285 else if (!strncmp(argv[2], "id:", 3))
286 print_id (doc, root, argv[2] + 3);
287 else if (!strcmp(argv[2], "labels"))