python 解析xml文件-python parse xml.
File:test.xml
本文件采用的是xml.etree.ElementTree 进行解析的。
1 <?xml version="1.0"?> 2 <mysqlconfig> 3 <database> 4 <host>127.0.0.1</host> 5 <username>root</username> 6 <password>123456</password> 7 <port>3306</port> 8 <instance name="test"> 9 <export-import tablename="test"> 10 <exportConfigure>export 1.1.1</exportConfigure> 11 <exportDelimiter>|</exportDelimiter> 12 <importConfigure>import 1.1</importConfigure> 13 </export-import> 14 <export-import tablename="test"> 15 <exportConfigure>export 1.1.2</exportConfigure> 16 <exportDelimiter>|</exportDelimiter> 17 <importConfigure>import 1.1.2</importConfigure> 18 </export-import> 19 </instance> 20 <instance name="test1"> 21 <export-import tablename="users"> 22 <exportConfigure>export 1.2.1</exportConfigure> 23 <exportDelimiter>|</exportDelimiter> 24 <importConfigure>import 1.2.1</importConfigure> 25 </export-import> 26 <export-import tablename="sss"> 27 <exportConfigure>export 1.2.2</exportConfigure> 28 <importConfigure>import 1.2.2</importConfigure> 29 </export-import> 30 </instance> 31 </database> 32 <database> 33 <host>127.0.0.1</host> 34 <username>test</username> 35 <password>test</password> 36 <port>3306</port> 37 <instance name="test_it"> 38 <export-import tablename="user_info"> 39 <exportConfigure>select * from test </exportConfigure> 40 <exportDelimiter>|</exportDelimiter> 41 <exportFilePrefix>./data/</exportFilePrefix> 42 <importConfigure>import 2.2.1</importConfigure> 43 </export-import> 44 </instance> 45 </database> 46 </mysqlconfig>
python 解析文件:parse.py
1 import xml.etree.ElementTree as ET 2 3 class ExportImportConfig: 4 def __init__(self): 5 self.tablename = None 6 self.exportConfig = None 7 self.exportDelimiter= None 8 self.exportFilePrefix = None 9 self.importExport = None 10 class InstanceConfig: 11 def __init__(self): 12 self.instanceName = None 13 self.ExportImportConfigList = [] #contains a list of InstanceConfig 14 15 class DatabaseConfig: 16 def __init__(self): 17 self.host = None 18 self.user = None 19 self.password = None 20 self.port = None 21 self.instanceConfigList = [] #contains a list of InstanceConfig 22 23 24 def getMysqlImportExportConfig(): 25 tree = ET.parse('mysql_export_source_config.xml') 26 root = tree.getroot() 27 databaseList =[] 28 for db in root.iter('database'): 29 database = DatabaseConfig() 30 database.host = db.find('host').text 31 database.user = db.find('username').text 32 database.password = db.find('password').text 33 database.port = int(db.find('port').text) 34 database.instanceConfigList = [] 35 for inst in db.iter('instance'): 36 instance = InstanceConfig() 37 instance.instanceName = inst.get('name') 38 for ex_im in db.iter('export-import'): 39 ex_im_config = ExportImportConfig() 40 ex_im_config.tablename = ex_im.get('tablename') 41 ex_im_config.exportConfig = ex_im.find('exportConfigure').text 42 ex_im_config.exportDelimiter = ex_im.find('exportDelimiter').text 43 ex_im_config.importExport = ex_im.find('importConfigure').text 44 ex_im_config.exportFilePrefix = ex_im.find('exportFilePrefix').text 45 # print ex_im_config.tablename,ex_im_config.exportConfig ,ex_im_config.importExport 46 print ex_im_config.exportDelimiter,ex_im_config.exportFilePrefix 47 instance.ExportImportConfigList.append(ex_im_config) 48 database.instanceConfigList.append(instance) 49 databaseList.append(database) 50 return databaseList 51 52 if __name__=='__main__': 53 exportlist = getMysqlImportExportConfig() 54 print 'lend exportlist =' ,len(exportlist) 55 for db in exportlist: 56 print db.user,db.password,db.port 57 for inst in db.instanceConfigList: 58 print db.instanceConfigList
posted on 2012-09-22 18:37 algorithmic 阅读(6035) 评论(0) 编辑 收藏 举报