Next Step: Show the Document Element's Attributes
<<<< ^^^^^ >>>>

Add import string, sub asciiNodeType(), sub printNodeInfo(), and call printNodeInfo().

#!/usr/bin/python
from xml.dom.ext.reader.Sax import FromXmlFile
from xml.dom import Node,Element,Document,Text
import sys
import string

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):
   __textType=asciiNodeType(mynode.nodeType);
   __value=(mynode.nodeValue);
   print asciiNodeType(mynode._get_nodeType()) + ' :',
   print mynode._get_nodeName() + ' :',
   print mynode._get_nodeValue(),
   if __textType == 'ELEMENT':
      __attrList = mynode.attributes;
      print '(', 
      for __ss in range(0, __attrList._get_length()):
         __attrName = __attrList.keys()[__ss];
         __attrValue = mynode.getAttribute(__attrName);
         print __attrName + '="' + __attrValue + '"',
      print ') ', 
   print '.'

__domObject = FromXmlFile(sys.argv[1])
print __domObject.documentElement
printNodeInfo(__domObject.documentElement)

<<<< ^^^^^ >>>>
 
 
 
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.
 

21.html

Exit Slideshow