Python:xml讀取(xml.dom.mindom)
阿新 • • 發佈:2018-12-17
# coding:UTF-8 import os from xml.dom import minidom class ConfigException(Exception): ''' 配置檔案例外 ''' pass class ReadXml(): ''' 讀取xml資訊 ''' def __init__(self,file = None): if file == None: xmlPath = os.path.split(os.path.realpath(__file__))[0] + "\\config.xml" else: xmlPath = file print(xmlPath) try: root = minidom.parse(xmlPath) self.dom = root.documentElement except Exception as err: raise ConfigException("xml讀取異常",xmlPath) def db_server_info(self): db_dom = self.dom.getElementsByTagName("db_server") db_info = {} db_info["ip"] = db_dom[0].getAttribute("ip") db_info["port"] = db_dom[0].getAttribute("port") db_info["name"] = db_dom[0].getAttribute("name") db_info["user"] = db_dom[0].getAttribute("user") db_info["passwd"] = db_dom[0].getAttribute("passwd") return db_info def rest_server_info(self): rest_dom = self.dom.getElementsByTagName("rest_server") rest_info = {} rest_info["ip"] = rest_dom[0].getAttribute("ip") rest_info["port"] = rest_dom[0].getAttribute("port") return rest_info def thrift_server_info(self): thrift_dom = self.dom.getElementsByTagName("thrift_server") thrift_info = {} thrift_info["ip"] = thrift_dom[0].getAttribute("ip") thrift_info["port"] = thrift_dom[0].getAttribute("port") return thrift_info def path_info(self): path_dom = self.dom.getElementsByTagName("path") path_info = {} device_dom = path_dom[0].getElementsByTagName("device")[0] path_info["device"] = device_dom.firstChild.data preprocess_dom = path_dom[0].getElementsByTagName("preprocess")[0] path_info["preprocess"] = device_dom.firstChild.data match_dom = path_dom[0].getElementsByTagName("match")[0] path_info["match"] = device_dom.firstChild.data facedb_dom = path_dom[0].getElementsByTagName("facedb")[0] path_info["facedb"] = device_dom.firstChild.data person_dom = path_dom[0].getElementsByTagName("person")[0] path_info["person"] = device_dom.firstChild.data require_dom = path_dom[0].getElementsByTagName("require")[0] path_info["require"] = device_dom.firstChild.data facepic_dom = path_dom[0].getElementsByTagName("facepic")[0] path_info["facepic"] = device_dom.firstChild.data video_dom = path_dom[0].getElementsByTagName("video")[0] path_info["video"] = device_dom.firstChild.data grab_dom = path_dom[0].getElementsByTagName("grab_remark")[0] path_info["grab_remark"] = device_dom.firstChild.data return path_info def camera_info(self): camera_dom = self.dom.getElementsByTagName("camera_device") cameras_info = [] for camer in camera_dom: camera_info = {} camera_info["camera_device"] = camer.getAttribute("ip") camera_info["port"] = camer.getAttribute("port") camera_info["user"] = camer.getAttribute("user") camera_info["passwd"] = camer.getAttribute("passwd") cameras_info.append(camera_info) return cameras_info def mailto_info(self): mailto_dom = self.dom.getElementsByTagName("mail_to") mailto_info = [mailto.firstChild.data for mailto in mailto_dom] return mailto_info if __name__ == "__main__": c = ReadXml() print(c.mailto_info())
要讀取的xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<db_server ip="192.168.29.142" port="3306" name="opzoon_api_0422" user="opzoon" passwd="123.com">
</db_server>
<rest_server ip="192.168.29.142" port="5000">
</rest_server>
<thrift_server ip="192.168.29.142" port="5050">
</thrift_server>
<thrift_match ip="192.168.29.142" port="5051">
</thrift_match>
<thrift_preprocess ip="192.168.29.142" port="5052">
</thrift_preprocess>
<path>
<device>/v2/devices</device>
<preprocess>/v2/collections</preprocess>
<match>/v2/matchs</match>
<facedb>/v2/facedbs</facedb>
<person>/v2/persons</person>
<require>/v2/facesearchresult</require>
<facepic>/v2/facepic</facepic>
<video>/v2/videos</video>
<grab_remark>/v2/grab_remark</grab_remark>
</path>
<camera_device ip="192.168.30.227" port='80' user="admin" passwd="opzoon123456">
</camera_device>
<camera_device ip="192.168.50.190" port='80' user="admin" passwd="12345">
</camera_device>
<camera_device_invalid ip="10.10.10.10" port='80' user="admin" passwd="12345">
</camera_device_invalid>>
<mail_from user=" [email protected]" passwd="pmbfzpanmiqjyias">
</mail_from>
<mail_to>[email protected]</mail_to>
<mail_to>[email protected]</mail_to>
</catalog>