python读取xml数据库中表内所有数据,获取数据库中所有表的字段名称

工作中需要读取指定xml数据库中的数据以及 表所需字段名,所以在已有例子中改进实现:

xml 数据库 xmldabase.xml:

 

[html] 

  1. <database>
  2.     <manifest>
  3.         <pair key=”schema_major_vsn” value=”5″/>
  4.         <pair key=”schema_minor_vsn” value=”63″/>
  5.         <pair key=”generation_count” value=”8541968″/>
  6.     </manifest>
  7.     <table name=”PCI”>
  8.         <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.”/>
  9.     </table>
  10. <table name=”xx_metrics”>
  11.         <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”/>
  12.         <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”/>
  13.         <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”/>
  14.         <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”/>
  15.     </table>
  16. </database>

获取指定表的数据:

 

 

[python] 

  1. from xml.etree import ElementTree
  2. def get_table_records( xmlfile = None,text = None,table_name=None):
  3.     root=None
  4.     if xmlfile:
  5.         root = ElementTree.parse(xmlfile)
  6.     elif text:
  7.         root = ElementTree.fromstring(text)
  8.     node_findall = root.findall(“table”)
  9.     for e in node_findall:
  10.         for key,value in e.items():
  11.             if value==table_name:
  12.                 print( “%s:%s” % (key, value)   )
  13.                 ar=e.findall(‘row’)
  14.                 for row in ar:
  15.                     for k,v in row.items():
  16.                         print( “%s:%s” % (k, v)   )

获取所有表的字段名称:

 

 

[python]

  1. def get_table_field(text =None, xmlfile =None):
  2.     root=None
  3.     if xmlfile:
  4.         root = ElementTree.parse(xmlfile)
  5.     elif text:
  6.         root = ElementTree.fromstring(text)
  7.     node_findall = root.findall(“table”)
  8.     for e in node_findall:
  9.         list_field=[]
  10.         for key,value in e.items():
  11. #                print( “%s:%s” % (key, value)   )
  12.                 first_row=e.find(‘row’)
  13. #                print(type(first_row))
  14.                 if first_row==None :
  15.                     print(‘%s_need_fields=%s’%(value,list_field))
  16. #                    continue
  17.                 else:
  18.                     for k,v in first_row.items():
  19.                         list_field.append(k)
  20.     #                        print( “%s:%s” % (k, v)   )
  21.                     print(‘%s_need_fields=%s’%(value,list_field))

 

测试用例:

 

[python] 

  1. if __name__ == ‘__main__’:
  2. #    get_table_records(text=open(“xmldabase.xml”).read(),table_name=’PCI’)  #获取xml的方式不同,皆可用
  3.     get_table_records(xmlfile=”xmldabase.xml”,table_name=’PCI’)
  4.     get_table_field(xmlfile=”xmldabase.xml”)

输出结果:

 

 

 

[python]

  1. <pre></pre><pre name=”code” class=”python”>name:PCI
  2. class_name:Display controller
  3. vendor_id:1a03
  4. __mtime:8489775
  5. host:OpaqueRef:d92d7211-482f-c68f-f89b-4c8b22faec1a
  6. dependencies:()
  7. _ref:OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21d
  8. device_id:2000
  9. functions:1
  10. uuid:5907d30d-7d53-4682-4ff1-ed32974cb3b6
  11. class_id:03
  12. other_config:()
  13. __ctime:8489729
  14. pci_id:0000:09:00.0
  15. device_name:ASPEED Graphics Family
  16. attached_VMs:()
  17. vendor_name:ASPEED Technology, Inc.
  18. ref:OpaqueRef:1e2c98ee-a6fe-b1d5-e91c-d27911deb21d
  19. 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’]
  20. 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’]
  21. </pre>
  22. <pre></pre>
  23. <pre></pre>
  24. <pre></pre>

标签