#include <stdio.h>
+#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
{
for (xmlNode *node = a_node; node; node = node->next)
{
+ const xmlNode *parent = node->parent;
+ if (parent->type == XML_ELEMENT_NODE)
+ printf ("%s ", parent->name);
+ else
+ printf ("<root> ");
+
if (node->type == XML_ELEMENT_NODE)
- {
- const xmlNode *parent = node->parent;
- if (parent->type == XML_ELEMENT_NODE)
- printf ("%s %s\n", parent->name, node->name);
- else if (parent->type == XML_DOCUMENT_NODE)
- printf ("<root> %s\n", node->name);
- }
+ printf ("%s", node->name);
+ else if (node->type == XML_TEXT_NODE)
+ printf("<text>");
+ else if (node->type == XML_CDATA_SECTION_NODE)
+ printf("<cdata>");
+ else
+ printf("<other>");
+
+ putchar('\n');
print_containment (node->children);
}
}
-int
-main (int argc, char **argv)
+static void
+print_attributes (xmlNode * a_node)
{
- if (argc != 2)
+ for (xmlNode *node = a_node; node; node = node->next)
{
- fprintf (stderr, "usage: %s FILE.xml\n", argv[0]);
- exit (1);
+ for (xmlAttr *attr = node->properties; attr; attr = attr->next)
+ printf ("%s %s\n", node->name, attr->name);
+
+ print_attributes (node->children);
}
+}
+
+static void
+usage (void)
+{
+ fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
+ exit (1);
+}
+
+int
+main (int argc, char **argv)
+{
+ if (argc != 3)
+ usage ();
LIBXML_TEST_VERSION;
}
xmlNode *root = xmlDocGetRootElement(doc);
- print_containment(root);
+
+ if (!strcmp(argv[2], "containment"))
+ print_containment (root);
+ else if (!strcmp(argv[2], "attributes"))
+ print_attributes (root);
+ else
+ usage ();
+
xmlFreeDoc(doc);
xmlCleanupParser();