类型转换和获取XML文件中保存的数据

前端之家收集整理的这篇文章主要介绍了类型转换和获取XML文件中保存的数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一.类型转换

在上一个博文解析了XML文件之后,由于解析之后得到的数据都是Unicode类型的,在一些场合并不适用,因此,需要将获得的数据进行类型转换,主要是将Unicode转换为int,string和float类型,主要代码如下:

  1. '''
  2. Created on 2014-6-20
  3.  
  4. @author: sheng
  5. '''
  6.  
  7. def ConvertUnicodeToInt(InputUnicode):
  8. """Convert the Unicode to integer"""
  9. result = int(InputUnicode)
  10. return result
  11.  
  12.  
  13.  
  14.  
  15. def ConvertUnicodeToString(InputUnicode):
  16. """Convert the Unicode to string"""
  17. result = str(InputUnicode)
  18. return result
  19.  
  20.  
  21.  
  22. def ConvertUnicodeToFloat(InputUnicode):
  23. """Convert the Unicode to InputUnicode"""
  24. result = float(InputUnicode)
  25. return result
  26.  
  27.  


二.获取XML文件中保存的数据

结合了XML解析和类型转换之后,就可以从XML文件中通过名字来直接获得标签保存的数据了,例如整数、字符串和浮点数,以及由这些类型组成的整数列表、字符串列表和浮点数列表。
具体代码如下:
  1. '''
  2. Created on 2014-6-20
  3.  
  4. @author: sheng
  5. '''
  6.  
  7.  
  8. import xml.dom.minidom
  9. from ConvertTypeXToTypeY import ConvertUnicodeToInt
  10. from ConvertTypeXToTypeY import ConvertUnicodeToString
  11. from ConvertTypeXToTypeY import ConvertUnicodeToFloat
  12.  
  13.  
  14. class XMLParser(object):
  15. '''
  16. classdocs
  17. '''
  18.  
  19.  
  20. def __init__(self,FileName):
  21. '''
  22. Constructor
  23. '''
  24. # open the xml file
  25. self.Dom = xml.dom.minidom.parse(FileName)
  26. # get the root element
  27. self.RootElement = self.Dom.documentElement
  28.  
  29. def GetInt(self,ElementName):
  30. """
  31. Get the integer by name
  32. """
  33. # get the element list
  34. ElementList = self.GetData(ElementName)
  35. # get the data
  36. RawResult = ElementList[0].firstChild.data
  37.  
  38. # convert the data to integer from Unicode
  39. Result = ConvertUnicodeToInt(RawResult)
  40. return Result
  41. def GetIntList(self,ElementName):
  42. """
  43. Get the integer list by name
  44. """
  45. # get the element list
  46. ElementList = self.GetData(ElementName)
  47. Result = []
  48. # get the data
  49. for TmpElement in ElementList:
  50. Tmp = TmpElement.firstChild.data
  51. Result.append(ConvertUnicodeToInt(Tmp))
  52. return Result
  53. def GetString(self,ElementName):
  54. """
  55. Get the string by name
  56. """
  57. # get the element list
  58. ElementList = self.GetData(ElementName)
  59. # get the data
  60. RawResult = ElementList[0].firstChild.data
  61. # convert the data to integer from Unicode
  62. Result = ConvertUnicodeToString(RawResult)
  63. return Result
  64. def GetStringList(self,ElementName):
  65. """
  66. Get the float list by name
  67. """
  68. # get the element list
  69. ElementList = self.GetData(ElementName)
  70. Result = []
  71. # get the data
  72. for TmpElement in ElementList:
  73. Tmp = TmpElement.firstChild.data
  74. Result.append(ConvertUnicodeToString(Tmp))
  75. return Result
  76. def GetFloat(self,ElementName):
  77. """
  78. Get the float by name
  79. """
  80. # get the element list
  81. ElementList = self.GetData(ElementName)
  82. # get the data
  83. RawResult = ElementList[0].firstChild.data
  84. # convert the data to integer from Unicode
  85. Result = ConvertUnicodeToFloat(RawResult)
  86. return Result
  87. def GetFloatList(self,ElementName):
  88. """
  89. Get the string list by name
  90. """
  91. # get the element list
  92. ElementList = self.GetData(ElementName)
  93. Result = []
  94. # get the data
  95. for TmpElement in ElementList:
  96. Tmp = TmpElement.firstChild.data
  97. Result.append(ConvertUnicodeToFloat(Tmp))
  98. return Result
  99. def GetData(self,ElementName):
  100. """
  101. Get the data of the element by name
  102. """
  103. RawResult = self.RootElement.getElementsByTagName(ElementName)
  104. return RawResult



至此,XML文件解析和类型转换结束,可以使用上面的代码来获得xml文件中保存的整数、字符串和浮点数,以及这些类型对应的列表了。
欢迎大家批评指正~
Enjoy it ~


感谢下面的网友的无私分享
http://www.cnblogs.com/jenry/archive/2010/05/27/1744861.html

猜你在找的XML相关文章