python讀取xml資料庫中表內所有資料,擷取資料庫中所有表的欄位名稱

來源:互聯網
上載者:User

工作中需要讀取指定xml資料庫中的資料以及 表所需欄位名,所以在已有例子中改進實現:

xml 資料庫 xmldabase.xml:

<database><manifest><pair key="schema_major_vsn" value="5"/><pair key="schema_minor_vsn" value="63"/><pair key="generation_count" value="8541968"/></manifest><table name="PCI"><row ref="OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21d" __ctime="8489729" __mtime="8489775" _ref="OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21d" attached_VMs="()" class_id="03" class_name="Display controller" dependencies="()" device_id="2000" device_name="ASPEED Graphics Family" functions="1" host="OpaqueRef:d92d7211-482f-c68f-f89b-4c8b22faec1a" other_config="()" pci_id="0000:09:00.0" uuid="5907d30d-7d53-4682-4ff1-ed32974cb3b6" vendor_id="1a03" vendor_name="ASPEED Technology, Inc."/></table><table name="xx_metrics"><row ref="OpaqueRef:5105a1b6-a689-15a9-708d-57417c69d4b4" __ctime="8489729" __mtime="8490755" _ref="OpaqueRef:5105a1b6-a689-15a9-708d-57417c69d4b4" carrier="true" device_id="10d3" device_name="82574L Gigabit Network Connection" duplex="true" io__read_kbs="0." io__write_kbs="0." last_updated="20130508T00:02:46Z" other_config="()" pci_bus_path="0000:06:00.0" speed="1000" uuid="a26a8b42-b4c1-1b3c-1236-ea62d2d05236" vendor_id="8086" vendor_name="Intel Corporation"/><row ref="OpaqueRef:63f81794-55b1-af60-5225-900673b300d2" __ctime="8489729" __mtime="8490752" _ref="OpaqueRef:63f81794-55b1-af60-5225-900673b300d2" carrier="false" device_id="10d3" device_name="82574L Gigabit Network Connection" duplex="false" io__read_kbs="0." io__write_kbs="0." last_updated="20130508T00:02:46Z" other_config="()" pci_bus_path="0000:07:00.0" speed="65535" uuid="74bc3019-9c04-ca9d-6ea8-3be63840620d" vendor_id="8086" vendor_name="Intel Corporation"/><row ref="OpaqueRef:a0e992d8-5293-581a-0ad1-2b9a00c53d2a" __ctime="8489729" __mtime="8490754" _ref="OpaqueRef:a0e992d8-5293-581a-0ad1-2b9a00c53d2a" carrier="true" device_id="10d3" device_name="82574L Gigabit Network Connection" duplex="true" io__read_kbs="0." io__write_kbs="0." last_updated="20130508T00:02:46Z" other_config="()" pci_bus_path="0000:06:00.0" speed="1000" uuid="fc9d0a76-eb60-dfc8-18b6-bfe26fe8be9d" vendor_id="8086" vendor_name="Intel Corporation"/><row ref="OpaqueRef:f9374ed7-316b-0f53-92d1-f7f70823f6ce" __ctime="8489729" __mtime="8490753" _ref="OpaqueRef:f9374ed7-316b-0f53-92d1-f7f70823f6ce" carrier="true" device_id="10d3" device_name="82574L Gigabit Network Connection" duplex="true" io__read_kbs="0." io__write_kbs="0." last_updated="20130508T00:02:46Z" other_config="()" pci_bus_path="0000:06:00.0" speed="1000" uuid="e4f38332-1317-b9df-07f9-ab5681569c91" vendor_id="8086" vendor_name="Intel Corporation"/></table></database>

擷取指定表的資料:

from xml.etree import ElementTreedef get_table_records( xmlfile = None,text = None,table_name=None):    root=None    if xmlfile:        root = ElementTree.parse(xmlfile)    elif text:        root = ElementTree.fromstring(text)        node_findall = root.findall("table")    for e in node_findall:               for key,value in e.items():            if value==table_name:                print( "%s:%s" % (key, value)   )                ar=e.findall('row')                for row in ar:                    for k,v in row.items():                                                print( "%s:%s" % (k, v)   )

擷取所有表的欄位名稱:

def get_table_field(text =None, xmlfile =None):    root=None    if xmlfile:        root = ElementTree.parse(xmlfile)    elif text:        root = ElementTree.fromstring(text)        node_findall = root.findall("table")    for e in node_findall:        list_field=[]            for key,value in e.items():#                print( "%s:%s" % (key, value)   )                first_row=e.find('row')#                print(type(first_row))                if first_row==None :                    print('%s_need_fields=%s'%(value,list_field))#                    continue                else:                    for k,v in first_row.items():                        list_field.append(k)    #                        print( "%s:%s" % (k, v)   )                        print('%s_need_fields=%s'%(value,list_field))

測試案例:

if __name__ == '__main__':    #    get_table_records(text=open("xmldabase.xml").read(),table_name='PCI')  #擷取xml的方式不同,皆可用     get_table_records(xmlfile="xmldabase.xml",table_name='PCI')     get_table_field(xmlfile="xmldabase.xml") 

輸出結果:

name:PCIclass_name:Display controllervendor_id:1a03__mtime:8489775host:OpaqueRef:d92d7211-482f-c68f-f89b-4c8b22faec1adependencies:()_ref:OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21ddevice_id:2000functions:1uuid:5907d30d-7d53-4682-4ff1-ed32974cb3b6class_id:03other_config:()__ctime:8489729pci_id:0000:09:00.0device_name:ASPEED Graphics Familyattached_VMs:()vendor_name:ASPEED Technology, Inc.ref:OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21dPCI_need_fields=['class_name', 'vendor_id', '__mtime', 'host', 'dependencies', '_ref', 'device_id', 'functions', 'uuid', 'class_id', 'other_config', '__ctime', 'pci_id', 'device_name', 'attached_VMs', 'vendor_name', 'ref']xx_metrics_need_fields=['last_updated', 'vendor_id', '__mtime', '_ref', 'device_name', 'speed', 'device_id', 'uuid', 'duplex', 'other_config', '__ctime', 'pci_bus_path', 'io__write_kbs', 'carrier', 'io__read_kbs', 'vendor_name', 'ref']
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.