ATLAS Offline Software
Classes | Functions | Variables
python.xmldict Namespace Reference

Classes

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

Functions

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

Variables

 __doc__
 
 __author__
 
 etree
 
 __all__
 module data -------------------------------------------------------------— More...
 

Function Documentation

◆ _dict2xml_recurse()

def python.xmldict._dict2xml_recurse (   parent,
  dictitem 
)
private

Definition at line 98 of file xmldict.py.

98 def _dict2xml_recurse(parent, dictitem):
99  assert type(dictitem) is not type(list)
100 
101  if isinstance(dictitem, dict):
102  for (tag, child) in dictitem.iteritems():
103  if isinstance(child, str):
104  child = _xml_escape(child)
105  if str(tag) == '_text':
106  parent.text = str(child)
107  elif type(child) is type(list):
108  for listchild in child:
109  elem = ElementTree.Element(tag)
110  parent.append(elem)
111  _dict2xml_recurse (elem, listchild)
112  else:
113  elem = ElementTree.Element(tag)
114  parent.append(elem)
115  _dict2xml_recurse (elem, child)
116  else:
117  parent.text = str(dictitem)
118 

◆ _xml2dict_recurse()

def python.xmldict._xml2dict_recurse (   node,
  dictclass 
)
private

Definition at line 126 of file xmldict.py.

126 def _xml2dict_recurse (node, dictclass):
127  nodedict = dictclass()
128 
129  if len(node.items()) > 0:
130  # if we have attributes, set them
131  nodedict.update(dict((k, _xml_unescape(v) if isinstance(v, str) else v)
132  for k,v in node.items()))
133 
134  for child in node:
135  # recursively add the element's children
136  newitem = _xml2dict_recurse (child, dictclass)
137  if isinstance(newitem, str):
138  newitem = _xml_unescape(newitem)
139  if child.tag in nodedict:
140  # found duplicate tag, force a list
141  if isinstance(nodedict[child.tag], list):
142  # append to existing list
143  nodedict[child.tag].append(newitem)
144  else:
145  # convert to list
146  nodedict[child.tag] = [nodedict[child.tag], newitem]
147  else:
148  # only one, directly set the dictionary
149  nodedict[child.tag] = newitem
150 
151  if node.text is None:
152  text = ''
153  else:
154  text = node.text.strip()
155 
156  if len(nodedict) > 0:
157  # if we have a dictionary add the text as a dictionary value
158  # (if there is any)
159  if len(text) > 0:
160  nodedict['_text'] = text
161  else:
162  # if we don't have child nodes or attributes, just set the text
163  if node.text: nodedict = node.text.strip()
164  else: nodedict = ""
165 
166  return nodedict
167 

◆ dict2xml()

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

Definition at line 119 of file xmldict.py.

119 def dict2xml(xmldict):
120  """convert a python dictionary into an XML tree"""
121  roottag = xmldict.keys()[0]
122  root = ElementTree.Element(roottag)
123  _dict2xml_recurse (root, xmldict[roottag])
124  return root
125 

◆ import_etree()

def python.xmldict.import_etree ( )

Definition at line 16 of file xmldict.py.

16 def import_etree():
17  import xml
18  # first try the usual way
19  try:
20  import xml.etree
21  return xml.etree
22  except ImportError:
23  pass
24  # do it by hook or by crook...
25  import os, imp
26  xml_site_package = os.path.join(os.path.dirname(os.__file__), 'xml')
27  m = imp.find_module('etree', [xml_site_package])
28 
29  etree = imp.load_module('xml.etree', *m)
30  xml.etree = etree
31  return etree
32 

◆ xml2dict()

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

Definition at line 168 of file xmldict.py.

168 def xml2dict (root, dictclass=XmlDictObject):
169  """convert an xml tree into a python dictionary
170  """
171  return dictclass({root.tag: _xml2dict_recurse (root, dictclass)})

Variable Documentation

◆ __all__

python.xmldict.__all__
private

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

Definition at line 40 of file xmldict.py.

◆ __author__

python.xmldict.__author__
private

Definition at line 11 of file xmldict.py.

◆ __doc__

python.xmldict.__doc__
private

Definition at line 8 of file xmldict.py.

◆ etree

python.xmldict.etree

Definition at line 33 of file xmldict.py.

python.xmldict.dict2xml
def dict2xml(xmldict)
Definition: xmldict.py:119
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.xmldict.xml2dict
def xml2dict(root, dictclass=XmlDictObject)
Definition: xmldict.py:168
python.xmldict.import_etree
def import_etree()
Definition: xmldict.py:16
python.xmldict._xml2dict_recurse
def _xml2dict_recurse(node, dictclass)
Definition: xmldict.py:126
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.xmldict._dict2xml_recurse
def _dict2xml_recurse(parent, dictitem)
Definition: xmldict.py:98
str
Definition: BTagTrackIpAccessor.cxx:11