3 #include <libxml/parser.h>
4 #include <libxml/tree.h>
7 print_containment (xmlNode * a_node)
9 for (xmlNode *node = a_node; node; node = node->next)
11 const xmlNode *parent = node->parent;
12 if (parent->type == XML_ELEMENT_NODE)
13 printf ("%s ", parent->name);
17 if (node->type == XML_ELEMENT_NODE)
18 printf ("%s", node->name);
19 else if (node->type == XML_TEXT_NODE)
21 else if (node->type == XML_CDATA_SECTION_NODE)
28 print_containment (node->children);
33 print_attributes (xmlNode * a_node)
35 for (xmlNode *node = a_node; node; node = node->next)
37 for (xmlAttr *attr = node->properties; attr; attr = attr->next)
38 printf ("%s %s\n", node->name, attr->name);
40 print_attributes (node->children);
47 fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
52 main (int argc, char **argv)
59 xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
62 fprintf (stderr, "error: could not parse file %s\n", argv[1]);
66 xmlNode *root = xmlDocGetRootElement(doc);
68 if (!strcmp(argv[2], "containment"))
69 print_containment (root);
70 else if (!strcmp(argv[2], "attributes"))
71 print_attributes (root);