py2xml.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Copied from http://code.activestate.com/recipes/577268-python-data-structure-to-xml-serialization/
  8. # - indent feature and better formatting added
  9. # - fixed handling of lists in lists
  10. # - fixed root object name for dictionaries
  11. INDENT_SPACE_SYMBOL = " "
  12. '''
  13. Py2XML - Python to XML serialization
  14. This code transforms a Python data structures into an XML document
  15. Usage:
  16. serializer = Py2XML()
  17. xml_string = serializer.parse( python_object )
  18. print python_object
  19. print xml_string
  20. '''
  21. class Py2XML():
  22. def __init__( self ):
  23. self.data = "" # where we store the processed XML string
  24. def parse( self, pythonObj, objName=None, indent = 0 ):
  25. '''
  26. processes Python data structure into XML string
  27. needs objName if pythonObj is a List
  28. '''
  29. if pythonObj == None:
  30. return "\n" + (INDENT_SPACE_SYMBOL * indent) + ""
  31. if isinstance( pythonObj, dict ):
  32. self.data = self._PyDict2XML( pythonObj, objName, indent=indent+1 )
  33. elif isinstance( pythonObj, list ):
  34. # we need name for List object
  35. self.data = self._PyList2XML( pythonObj, objName, indent=indent+1 )
  36. else:
  37. self.data = "\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s>%(o)s</%(n)s>" % { 'n':objName, 'o':str( pythonObj ) }
  38. return self.data
  39. def _PyDict2XML( self, pyDictObj, objName=None, indent = 0 ):
  40. '''
  41. process Python Dict objects
  42. They can store XML attributes and/or children
  43. '''
  44. tagStr = "" # XML string for this level
  45. attributes = {} # attribute key/value pairs
  46. attrStr = "" # attribute string of this level
  47. childStr = "" # XML string of this level's children
  48. for k, v in pyDictObj.items():
  49. if isinstance( v, dict ):
  50. # child tags, with attributes
  51. childStr += self._PyDict2XML( v, k, indent=indent+1 )
  52. elif isinstance( v, list ):
  53. # child tags, list of children
  54. childStr += self._PyList2XML( v, k, indent=indent+1 )
  55. else:
  56. # tag could have many attributes, let's save until later
  57. attributes.update( { k:v } )
  58. if objName == None:
  59. return childStr
  60. # create XML string for attributes
  61. for k, v in sorted(attributes.items()):
  62. attrStr += " %s=\"%s\"" % ( k, v )
  63. # let's assemble our tag string
  64. if childStr == "":
  65. tagStr += "\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s%(a)s />" % { 'n':objName, 'a':attrStr }
  66. else:
  67. tagStr += ("\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s%(a)s>%(c)s" + "\n" + (INDENT_SPACE_SYMBOL * indent) + "</%(n)s>") % { 'n':objName, 'a':attrStr, 'c':childStr }
  68. return tagStr
  69. def _PyList2XML( self, pyListObj, objName=None, indent = 0 ):
  70. '''
  71. process Python List objects
  72. They have no attributes, just children
  73. Lists only hold Dicts or Strings
  74. '''
  75. tagStr = "" # XML string for this level
  76. childStr = "" # XML string of children
  77. for childObj in pyListObj:
  78. if isinstance( childObj, dict ):
  79. # here's some Magic
  80. # we're assuming that List parent has a plural name of child:
  81. # eg, persons > person, so cut off last char
  82. # name-wise, only really works for one level, however
  83. # in practice, this is probably ok
  84. childStr += self._PyDict2XML( childObj, objName[:-1], indent=indent+1 )
  85. elif isinstance( childObj, list ):
  86. # here's some Magic
  87. # we're assuming that List parent has a plural name of child:
  88. # eg, persons > person, so cut off last char
  89. # name-wise, only really works for one level, however
  90. # in practice, this is probably ok
  91. childStr += self._PyList2XML( childObj, objName[:-1], indent=indent+1 )
  92. pass
  93. else:
  94. childStr += "\n" + (INDENT_SPACE_SYMBOL * (indent + 1)) + "<" + objName[:-1] + ">"
  95. for string in childObj:
  96. childStr += str(string);
  97. childStr += "</" + objName[:-1] + ">"
  98. if objName == None:
  99. return childStr
  100. tagStr += ("\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s>%(c)s" + "\n" + (INDENT_SPACE_SYMBOL * indent) + "</%(n)s>") % { 'n':objName, 'c':childStr }
  101. return tagStr