工作中需要读取指定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 ElementTree def 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:PCI class_name:Display controller vendor_id:1a03 __mtime:8489775 host:OpaqueRef:d92d7211-482f-c68f-f89b-4c8b22faec1a dependencies:() _ref:OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21d device_id:2000 functions:1 uuid:5907d30d-7d53-4682-4ff1-ed32974cb3b6 class_id:03 other_config:() __ctime:8489729 pci_id:0000:09:00.0 device_name:ASPEED Graphics Family attached_VMs:() vendor_name:ASPEED Technology, Inc. ref:OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21d PCI_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']作者:chenyulancn 发表于2013-5-17 11:00:04 原文链接阅读:23 评论:0 查看评论