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->properties && node->parent->type != XML_DOCUMENT_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_attribute (xmlNode *node, const char *attr)
85 for (; node; node = node->next)
87 const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
91 print_attribute (node->children, attr);
98 fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
103 main (int argc, char **argv)
110 xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
113 fprintf (stderr, "error: could not parse file %s\n", argv[1]);
117 xmlNode *root = xmlDocGetRootElement(doc);
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"))