3 #include <libxml/parser.h>
4 #include <libxml/tree.h>
7 print_containment (xmlNode *node)
9 for (; node; node = node->next)
11 if (node->type == XML_ELEMENT_NODE)
13 printf ("%s", node->name);
14 for (xmlNode *child = node->children; child; child = child->next)
17 if (child->type == XML_ELEMENT_NODE)
19 printf ("%s", child->name);
23 && child->next->type == XML_ELEMENT_NODE
24 && !strcmp((char *) child->name, (char *) child->next->name))
32 else if (child->type == XML_TEXT_NODE)
34 else if (child->type == XML_CDATA_SECTION_NODE)
37 printf ("<%d>", child->type);
42 print_containment (node->children);
47 print_labels (xmlNode *node)
49 for (; node; node = node->next)
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"))
56 for (xmlNode *child = node->children; child; child = child->next)
57 if (child->type == XML_TEXT_NODE)
58 puts ((char *) child->content);
61 print_labels (node->children);
66 print_attributes (xmlNode * a_node)
68 for (xmlNode *node = a_node; node; node = node->next)
70 if (node->type == XML_ELEMENT_NODE)
72 printf ("%s", node->name);
73 for (xmlAttr *attr = node->properties; attr; attr = attr->next)
74 printf (" %s", attr->name);
78 print_attributes (node->children);
83 print_string(xmlChar *s)
85 for (char *p = (char *) s; *p; p++)
93 print_cdata (xmlNode * a_node)
95 for (xmlNode *node = a_node; node; node = node->next)
97 if (node->type == XML_CDATA_SECTION_NODE)
99 print_string (node->content);
103 print_cdata (node->children);
108 print_attribute (xmlNode *node, const char *attr)
110 for (; node; node = node->next)
112 const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
114 printf ("%s %s=%s\n", node->name, attr, s);
116 print_attribute (node->children, attr);
120 static __attribute__((unused)) xmlNode *
121 find_page_setup (xmlNode *node)
123 for (; node; node = node->next)
125 if (node->name && !strcmp ((char *) node->name, "pageSetup"))
128 xmlNode *ps = find_page_setup (node->children);
136 print_text (xmlNode *node)
138 for (; node; node = node->next)
140 if (node->type == XML_ELEMENT_NODE)
142 printf ("%s", node->name);
143 for (xmlNode *child = node->children; child; child = child->next)
144 if (child->type == XML_TEXT_NODE)
147 print_string (child->content);
152 print_text (node->children);
159 fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
164 main (int argc, char **argv)
171 xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
174 fprintf (stderr, "error: could not parse file %s\n", argv[1]);
178 xmlNode *root = xmlDocGetRootElement(doc);
181 /* Limit what we look at to pageSetup node and below. */
182 root = find_page_setup(root);
188 if (!strcmp(argv[2], "containment"))
189 print_containment (root);
190 else if (!strcmp(argv[2], "attributes"))
191 print_attributes (root);
192 else if (!strcmp(argv[2], "cdata"))
194 else if (!strcmp(argv[2], "text"))
196 else if (!strncmp(argv[2], "attr:", 5))
197 print_attribute (root, argv[2] + 5);
198 else if (!strcmp(argv[2], "labels"))