XML Walker's Source Code
<<<< ^^^^^ >>>>
#!/usr/bin/python
from xml.dom.ext.reader.Sax import FromXmlFile
from xml.dom import Node,Element,Document,Text
import sys;
import string;

def delBlankTextNodes(mynode):
   __kidsList = mynode._get_childNodes()
   __numKids = len(__kidsList)
   __ss = 0
   while __ss < __numKids:
      __child = __kidsList[__ss]
      if asciiNodeType(__child._get_nodeType()) == 'TEXT' and \
                   string.strip(str(__child._get_nodeValue())) == '':
         mynode.removeChild(__child);
         __numKids = __numKids - 1 
      else:
         delBlankTextNodes(__child);
         __ss = __ss + 1

def asciiNodeType(n):
   nodenames=['ELEMENT','ATTRIBUTE','TEXT',
       'CDATA_SECTION','ENTITY_REFERENCE',
       'ENTITY','PROCESSING_INSTRUCTION',
       'COMMENT','DOCUMENT','DOCUMENT_TYPE',
       'DOCUMENT_FRAGMENT','NOTATION'];
   return(nodenames[n-1]);

def printNodeInfo(mynode):
   __asciiNodeType=asciiNodeType(mynode._get_nodeType());
   __value=(mynode._get_nodeValue());
   print __asciiNodeType + ' :',
   print mynode._get_nodeName() + ' :',
   print mynode._get_nodeValue(),
   if __asciiNodeType == 'ELEMENT':
      __attrList = mynode._get_attributes();
      print '(', 
      for __ss in range(0, __attrList._get_length()):
         __attrName = __attrList.keys()[__ss];
         __attrValue = mynode.getAttribute(__attrName);
         print __attrName + '="' + __attrValue + '"',
      print ') ', 
   print '.'

def indentToLevel(level):
   if level > 1:
      print ' ' *  (2*level-3),   

def print_nodes(mynode):
   __level=1
   __ascending=0
   while 1:
      if not __ascending:
         indentToLevel(__level)
         printNodeInfo(mynode)  
         
      if mynode._get_firstChild() and not __ascending:
         mynode=mynode._get_firstChild()
         __ascending = 0
         __level = __level+1
      elif mynode._get_nextSibling():
         mynode=mynode._get_nextSibling()
         __ascending = 0
      elif mynode._get_parentNode():
         mynode=mynode._get_parentNode()
         __ascending = 1
         __level = __level-1
      else:
         break
      

def parseToDOM(fname):
   return FromXmlFile(fname)

def main():
   if len(sys.argv) < 2:
      print "Usage: sys.argv[0] xmlFilename";
      sys.exit();
   print "Exploring " + sys.argv[1]

   __domObject=parseToDOM(sys.argv[1]);
   delBlankTextNodes(__domObject._get_documentElement())
   print_nodes(__domObject._get_documentElement());

main();

<<<< ^^^^^ >>>>
 
 
 
Copyright (C) 2000 by Steve Litt, you can freely distribute unmodified copies if this copyright notice and disclaimer are intact.  This material originally appeared on Troubleshooters.Com.

This presentation has no warrantee, express or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the presentation is with you.
 

25.html

Exit Slideshow