类型转换和获取XML文件中保存的数据
发布时间:2020-12-16 09:41:29 所属栏目:百科 来源:网络整理
导读:一.类型转换 在上一个 博文 解析了XML文件之后,由于解析之后得到的数据都是Unicode类型的,在一些场合并不适用,因此,需要将获得的数据进行类型转换,主要是将Unicode转换为int,string和float类型,主要代码如下: '''Created on 2014-6-20@author: sheng'
一.类型转换
在上一个博文解析了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文件中保存的数据
结合了XML解析和类型转换之后,就可以从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
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |