ATLAS Offline Software
Loading...
Searching...
No Matches
python.xmldict Namespace Reference

Classes

class  XmlDictObject
 module implementation ---------------------------------------------------— More...

Functions

 _dict2xml_recurse (parent, dictitem)
 dict2xml (xmldict)
 _xml2dict_recurse (node, dictclass)
 xml2dict (root, dictclass=XmlDictObject)

Variables

str __doc__
str __author__ = "Sebastien Binet <binet@cern.ch>"
list __all__
 module data -------------------------------------------------------------—

Function Documentation

◆ _dict2xml_recurse()

python.xmldict._dict2xml_recurse ( parent,
dictitem )
protected

Definition at line 78 of file xmldict.py.

78def _dict2xml_recurse(parent, dictitem):
79 assert type(dictitem) is not type(list)
80
81 if isinstance(dictitem, dict):
82 for (tag, child) in dictitem.iteritems():
83 if isinstance(child, str):
84 child = _xml_escape(child)
85 if str(tag) == '_text':
86 parent.text = str(child)
87 elif type(child) is type(list):
88 for listchild in child:
89 elem = ElementTree.Element(tag)
90 parent.append(elem)
91 _dict2xml_recurse (elem, listchild)
92 else:
93 elem = ElementTree.Element(tag)
94 parent.append(elem)
95 _dict2xml_recurse (elem, child)
96 else:
97 parent.text = str(dictitem)
98

◆ _xml2dict_recurse()

python.xmldict._xml2dict_recurse ( node,
dictclass )
protected

Definition at line 106 of file xmldict.py.

106def _xml2dict_recurse (node, dictclass):
107 nodedict = dictclass()
108
109 if len(node.items()) > 0:
110 # if we have attributes, set them
111 nodedict.update(dict((k, _xml_unescape(v) if isinstance(v, str) else v)
112 for k,v in node.items()))
113
114 for child in node:
115 # recursively add the element's children
116 newitem = _xml2dict_recurse (child, dictclass)
117 if isinstance(newitem, str):
118 newitem = _xml_unescape(newitem)
119 if child.tag in nodedict:
120 # found duplicate tag, force a list
121 if isinstance(nodedict[child.tag], list):
122 # append to existing list
123 nodedict[child.tag].append(newitem)
124 else:
125 # convert to list
126 nodedict[child.tag] = [nodedict[child.tag], newitem]
127 else:
128 # only one, directly set the dictionary
129 nodedict[child.tag] = newitem
130
131 if node.text is None:
132 text = ''
133 else:
134 text = node.text.strip()
135
136 if len(nodedict) > 0:
137 # if we have a dictionary add the text as a dictionary value
138 # (if there is any)
139 if len(text) > 0:
140 nodedict['_text'] = text
141 else:
142 # if we don't have child nodes or attributes, just set the text
143 if node.text: nodedict = node.text.strip()
144 else: nodedict = ""
145
146 return nodedict
147

◆ dict2xml()

python.xmldict.dict2xml ( xmldict)
convert a python dictionary into an XML tree

Definition at line 99 of file xmldict.py.

99def dict2xml(xmldict):
100 """convert a python dictionary into an XML tree"""
101 roottag = xmldict.keys()[0]
102 root = ElementTree.Element(roottag)
103 _dict2xml_recurse (root, xmldict[roottag])
104 return root
105

◆ xml2dict()

python.xmldict.xml2dict ( root,
dictclass = XmlDictObject )
convert an xml tree into a python dictionary

Definition at line 148 of file xmldict.py.

148def xml2dict (root, dictclass=XmlDictObject):
149 """convert an xml tree into a python dictionary
150 """
151 return dictclass({root.tag: _xml2dict_recurse (root, dictclass)})

Variable Documentation

◆ __all__

list python.xmldict.__all__
private
Initial value:
1= [
2 'xml2dict',
3 'dict2xml',
4 ]

module data -------------------------------------------------------------—

Definition at line 20 of file xmldict.py.

◆ __author__

str python.xmldict.__author__ = "Sebastien Binet <binet@cern.ch>"
private

Definition at line 11 of file xmldict.py.

◆ __doc__

str python.xmldict.__doc__
private
Initial value:
1= """\
2functions to convert an XML file into a python dict, back and forth
3"""

Definition at line 8 of file xmldict.py.