Python reads all the data in the table in the XML database and obtains the field names of all tables in the database.

Source: Internet
Author: User

In the work, you need to read the data in the specified XML database and the field names required for the table. Therefore, we can improve the implementation in the existing example:

XML database 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>

Obtain the data of a specified table:

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)   )

Obtain the field names of all tables:

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))

Test cases:

If _ name _ = '_ main _': # get_table_records (text = open ("xmldabase. XML "). read (), table_name = 'pci ') # get_table_records (xmlfile = "xmldabase. XML ", table_name = 'pci ') get_table_field (xmlfile =" xmldabase. XML ")

Output result:

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']

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.