Package deefuzzer :: Package tools :: Module xmltodict'
[hide private]
[frames] | no frames]

Source Code for Module deefuzzer.tools.xmltodict'

 1  #!/usr/bin/env python 
 2   
 3  import xml.dom.minidom 
 4   
 5   
6 -def haschilds(dom):
7 # Checks whether an element has any childs 8 # containing real tags opposed to just text. 9 for childnode in dom.childNodes: 10 if childnode.nodeName != "#text" and childnode.nodeName != "#cdata-section": 11 return True 12 return False
13 14
15 -def indexchilds(dom, enc):
16 childsdict = dict() 17 for childnode in dom.childNodes: 18 name = childnode.nodeName.encode(enc) 19 if name == "#text" or name == "#cdata-section": 20 # ignore whitespaces 21 continue 22 if haschilds(childnode): 23 v = indexchilds(childnode, enc) 24 else: 25 v = childnode.childNodes[0].nodeValue.encode(enc) 26 if name in childsdict: 27 if isinstance(childsdict[name], dict): 28 # there is multiple instances of this node - convert to list 29 childsdict[name] = [childsdict[name]] 30 childsdict[name].append(v) 31 else: 32 childsdict[name] = v 33 return childsdict
34 35
36 -def xmltodict(data, enc=None):
37 dom = xml.dom.minidom.parseString(data.strip()) 38 return indexchilds(dom, enc)
39