Work on structure XML description.
[pspp] / parse-xml.c
index c0445052ed017f6ea80111568dc581884c69d4e1..efc7bb3569df0117bfec656c2739d283deb48c30 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
@@ -7,27 +8,51 @@ print_containment (xmlNode * a_node)
 {
   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;
 
@@ -39,7 +64,14 @@ main (int argc, char **argv)
     }
 
   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();