Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 10:05:10

0001 from xml.dom import minidom
0002 
0003 # iterator object to iterate over all the elements of a (dom.minidom) DOM tree, starting from a given element
0004 class DOMIterator:
0005   element = None
0006 
0007   # can be initialized with a DOM node or a DOM document (in the latter case the main element is used)
0008   def __init__(self, element = None):
0009     if isinstance(element, minidom.Document):
0010       self.element = element.documentElement
0011     elif isinstance(element, minidom.Node):
0012       self.element = element
0013     else:
0014       raise TypeError("type not supported by DOMIterator")
0015 
0016   def __iter__(self):
0017     return self
0018 
0019   def next(self):
0020     if self.element is None:
0021       raise StopIteration
0022 
0023     value = self.element
0024 
0025     if self.element.firstChild:
0026       self.element = self.element.firstChild
0027     elif self.element.nextSibling:
0028       self.element = self.element.nextSibling
0029     else:
0030       self.element = self.element.parentNode.nextSibling
0031 
0032     return value
0033 
0034 
0035 # remove text from DOM nodes, making it suitable for document.toprettyxml() 
0036 def dom_strip(document):
0037   for element in DOMIterator(document):
0038     if element.nodeType == 3:
0039       if element.nodeValue.count("\n") < 2:
0040         element.parentNode.removeChild(element)
0041         element.unlink()
0042       else:
0043         element.nodeValue = ""
0044