微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类型转换和获取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

原文地址:https://www.jb51.cc/xml/298448.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。