]> git.mxchange.org Git - flightgear.git/commitdiff
* add printxml, an example utility that walks an xml-tree and prints it
authorehofman <ehofman>
Tue, 28 Apr 2009 16:47:48 +0000 (16:47 +0000)
committerTim Moore <timoore@redhat.com>
Fri, 1 May 2009 22:44:22 +0000 (00:44 +0200)
    contenst

utils/xmlgrep/ChangeLog
utils/xmlgrep/Makefile.am
utils/xmlgrep/printxml.c [new file with mode: 0644]

index d7b5b37f6b4a72d2548fed15e4db097187c96f26..be69264318c3cfec62f5fb39edad8312ae1968bf 100644 (file)
@@ -1,5 +1,7 @@
 28-04-2009
   * changes to the code to allow walking the xml-tree using "*" as a node name
+  * add printxml, an example utility that walks an xml-tree and prints it
+    contenst
 
 27-04-2009
   * add xmlInitBuffer() for processing of a preallocated buffer
index cd314d4d81a7e2252752fc95f4e7c8fe11686bbe..160aca75d936d79c7c6dd73956544e242182b1cc 100644 (file)
@@ -1,3 +1,5 @@
-noinst_PROGRAMS = xmlgrep
+noinst_PROGRAMS = xmlgrep printxml
 
 xmlgrep_SOURCES = xmlgrep.c xml.c xml.h
+
+printxml_SOURCES = printxml.c xml.c
diff --git a/utils/xmlgrep/printxml.c b/utils/xmlgrep/printxml.c
new file mode 100644 (file)
index 0000000..c35f7f1
--- /dev/null
@@ -0,0 +1,89 @@
+
+#include <stdio.h>
+#include <malloc.h>
+#include "xml.h"
+
+void print_xml(void *);
+
+int main(int argc, char **argv)
+{
+  if (argc < 1)
+  {
+    printf("usage: printxml <filename>\n\n");
+  }
+  else
+  {
+    void *rid;
+
+    rid = xmlOpen(argv[1]);
+    if (rid)
+    {
+      unsigned int i, num;
+      void *xid;
+      xid = xmlMarkId(rid);
+      num = xmlNodeGetNum(xid, "*");
+      for (i=0; i<num; i++)
+      {
+        if (xmlNodeGetPos(rid, xid, "*", i) != 0)
+        {
+          char name[256];
+          xmlNodeCopyName(xid, (char *)&name, 256);
+          printf("<%s>\n", name);
+          print_xml(xid);
+          printf("\n</%s>\n", name);
+        }
+      }
+      free(xid);
+
+      xmlClose(rid);
+    }
+    else
+    {
+      printf("Error while opening file for reading: '%s'\n", argv[1]);
+    }
+  }
+}
+
+void print_xml(void *id)
+{
+  static int level = 1;
+  void *xid = xmlMarkId(id);
+  unsigned int i, num;
+  
+  num = xmlNodeGetNum(xid, "*");
+  if (num == 0)
+  {
+    char value[256];
+    int q;
+
+    q = xmlCopyString(xid, (char *)&value, 256);
+    if (q) printf("%s", value);
+  }
+  else
+  {
+    unsigned int i, q;
+    for (i=0; i<num; i++)
+    {
+      if (xmlNodeGetPos(id, xid, "*", i) != 0)
+      {
+        char name[256];
+
+        xmlNodeCopyName(xid, (char *)&name, 256);
+
+        printf("\n");
+        for(q=0; q<level; q++) printf(" ");
+        printf("<%s>", name);
+
+        level++;
+        print_xml(xid);
+        level--;
+
+        printf("</%s>", name);
+      }
+      else printf("error\n");
+    }
+    printf("\n");
+    for(q=1; q<level; q++) printf(" ");
+  }
+}