一.类型转换
在上一个博文解析了XML文件之后,由于解析之后得到的数据都是Unicode类型的,在一些场合并不适用,因此,需要将获得的数据进行类型转换,主要是将Unicode转换为int,string和float类型,主要代码如下:
- '''
- Created on 2014-6-20
- @author: sheng
- '''
- def ConvertUnicodeToInt(InputUnicode):
- """Convert the Unicode to integer"""
- result = int(InputUnicode)
- return result
- def ConvertUnicodeToString(InputUnicode):
- """Convert the Unicode to string"""
- result = str(InputUnicode)
- return result
- def ConvertUnicodeToFloat(InputUnicode):
- """Convert the Unicode to InputUnicode"""
- result = float(InputUnicode)
- return result
二.获取XML文件中保存的数据
- '''
- Created on 2014-6-20
- @author: sheng
- '''
- import xml.dom.minidom
- from ConvertTypeXToTypeY import ConvertUnicodeToInt
- from ConvertTypeXToTypeY import ConvertUnicodeToString
- from ConvertTypeXToTypeY import ConvertUnicodeToFloat
- class XMLParser(object):
- '''
- classdocs
- '''
- def __init__(self,FileName):
- '''
- Constructor
- '''
- # open the xml file
- self.Dom = xml.dom.minidom.parse(FileName)
- # get the root element
- self.RootElement = self.Dom.documentElement
- def GetInt(self,ElementName):
- """
- Get the integer by name
- """
- # get the element list
- ElementList = self.GetData(ElementName)
- # get the data
- RawResult = ElementList[0].firstChild.data
- # convert the data to integer from Unicode
- Result = ConvertUnicodeToInt(RawResult)
- return Result
- def GetIntList(self,ElementName):
- """
- Get the integer list by name
- """
- # get the element list
- ElementList = self.GetData(ElementName)
- Result = []
- # get the data
- for TmpElement in ElementList:
- Tmp = TmpElement.firstChild.data
- Result.append(ConvertUnicodeToInt(Tmp))
- return Result
- def GetString(self,ElementName):
- """
- Get the string by name
- """
- # get the element list
- ElementList = self.GetData(ElementName)
- # get the data
- RawResult = ElementList[0].firstChild.data
- # convert the data to integer from Unicode
- Result = ConvertUnicodeToString(RawResult)
- return Result
- def GetStringList(self,ElementName):
- """
- Get the float list by name
- """
- # get the element list
- ElementList = self.GetData(ElementName)
- Result = []
- # get the data
- for TmpElement in ElementList:
- Tmp = TmpElement.firstChild.data
- Result.append(ConvertUnicodeToString(Tmp))
- return Result
- def GetFloat(self,ElementName):
- """
- Get the float by name
- """
- # get the element list
- ElementList = self.GetData(ElementName)
- # get the data
- RawResult = ElementList[0].firstChild.data
- # convert the data to integer from Unicode
- Result = ConvertUnicodeToFloat(RawResult)
- return Result
- def GetFloatList(self,ElementName):
- """
- Get the string list by name
- """
- # get the element list
- ElementList = self.GetData(ElementName)
- Result = []
- # get the data
- for TmpElement in ElementList:
- Tmp = TmpElement.firstChild.data
- Result.append(ConvertUnicodeToFloat(Tmp))
- return Result
- def GetData(self,ElementName):
- """
- Get the data of the element by name
- """
- RawResult = self.RootElement.getElementsByTagName(ElementName)
- return RawResult
至此,XML文件解析和类型转换结束,可以使用上面的代码来获得xml文件中保存的整数、字符串和浮点数,以及这些类型对应的列表了。
欢迎大家批评指正~
Enjoy it ~
感谢下面的网友的无私分享:
http://www.cnblogs.com/jenry/archive/2010/05/27/1744861.html