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

一.类型转换

在上一个博文解析了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

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念