• 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 
  • 相关阅读:
    生活重心
    做自己才对,想多只会徒增烦恼
    列下计划,一个个实现吧
    公司搬迁
    限制文件的类型
    总结
    mvc mvp mvvm区别
    sessionStorage
    localStorage点击次数存储
    2016.09.01 html5兼容
  • 原文地址:https://www.cnblogs.com/algorithmic/p/2698283.html
Copyright © 2020-2023  润新知