python - How to parse an XML file to a list? -


i trying parse xml file list python. have looked @ solutions on site , others , not make them work me. have managed in laborious way seems stupid me. seems there should easier way.

i have tried adapt other peoples code suit needs not working not sure of reading.

this xml file:

<?xml version="1.0"?> <configuration>     <location name ="location">         <latitude>54.637348</latitude>         <lathemi>n</lathemi>         <longitude>5.829723</longitude>         <longhemi>w</longhemi>     </location>     <microphone name="microphone">         <sensitivity>-26.00</sensitivity>     </microphone>     <weighting name="weighting">         <cweight>68</cweight>         <aweight>2011</aweight>     </weighting>     <optionallevels name="optionallevels">         <l95>95</l95>         <l90>90</l90>         <l50>50</l50>         <l10>10</l10>         <l05>05</l05>         <fmax>fmax</fmax>     </optionallevels>     <averagingperiod name="averagingperiod">         <onemin>1</onemin>         <fivemin>5</fivemin>         <tenmin>10</tenmin>         <fifteenmin>15</fifteenmin>         <thirtymin>30</thirtymin>     </averagingperiod>     <timeweighting name="timeweighting">         <fast>fast</fast>         <slow>slow</slow>     </timeweighting>     <reboottime name="reboottime">         <midnight>midnight</midnight>         <sevenam>7am</sevenam>         <sevenpm>7pm</sevenpm>         <elevenpm>23pm</elevenpm>     </reboottime>     <remoteupload name="remoteupload">         <nointernet>nointernet</nointernet>         <vodafone>vodafone</vodafone>     </remoteupload> </configuration> 

and python program.

#!/usr/bin/python import xml.etree.elementtree et   import os try:     import celementtree et except importerror:     try:         import xml.etree.celementtree et     except importerror:         exit_err("failed import celementtree known place")  file_name = ('/home/mark/desktop/practice/config_settings.xml') full_file = os.path.abspath(os.path.join('data', file_name))  dom = et.parse(full_file)  tree = et.parse(full_file) root = tree.getroot()  location_settings = dom.findall('location') mic_settings = dom.findall('microphone') weighting = dom.findall('weighting') olevels = dom.findall('optionallevels') avg_period = dom.findall('averagingperiod') time_weight = dom.findall('timeweighting') reboot = dom.findall('reboottime') remote_upload = dom.findall('remoteupload')  in location_settings:      latitude = i.find('latitude').text     lathemi = i.find('lathemi').text     longitude = i.find('longitude').text     longhemi = i.find('longhemi').text   in mic_settings:     sensitivity = i.find('sensitivity').text  in weighting:     cweight = i.find('cweight').text     aweight = i.find('aweight').text  in olevels:     l95 = i.find('l95').text     l90 = i.find('l90').text     l50 = i.find('l50').text     l10 = i.find('l10').text     l05 = i.find('l05').text  in avg_period:     onemin = i.find('onemin').text     fivemin = i.find('fivemin').text     tenmin = i.find('tenmin').text     fifteenmin = i.find('fifteenmin').text     thirtymin = i.find('thirtymin').text  in time_weight:     fast = i.find('fast').text     slow = i.find('slow').text  in reboot:     midnight = i.find('midnight').text     sevenam = i.find('sevenam').text     sevenpm = i.find('sevenpm').text     elevenpm= i.find('elevenpm').text  in remote_upload:     nointernet = i.find('nointernet').text     vodafone = i.find('vodafone').text  config_list = [latitude,lathemi,longitude,longhemi,sensitivity,aweight,cweight,                 l95,l90,l50,l10,l05,onemin,fivemin,tenmin,fifteenmin,thirtymin,                 fast,slow,midnight,sevenam,sevenam,elevenpm,nointernet,vodafone] print(config_list) 

the problem you're posing isn't defined. xml structure doesn't conform list structure begin with. if you're new python, think best way go you're trying use xmltodict parse implicit schema in xml python data structures.

e.g.

import xmltodict xml = """<?xml version="1.0"?> <configuration>     <location name ="location">         <latitude>54.637348</latitude>         <lathemi>n</lathemi>         <longitude>5.829723</longitude>         <longhemi>w</longhemi>     </location>     <microphone name="microphone">         <sensitivity>-26.00</sensitivity>     </microphone>     <weighting name="weighting">         <cweight>68</cweight>         <aweight>2011</aweight>     </weighting>     <optionallevels name="optionallevels">         <l95>95</l95>         <l90>90</l90>         <l50>50</l50>         <l10>10</l10>         <l05>05</l05>         <fmax>fmax</fmax>     </optionallevels>     <averagingperiod name="averagingperiod">         <onemin>1</onemin>         <fivemin>5</fivemin>         <tenmin>10</tenmin>         <fifteenmin>15</fifteenmin>         <thirtymin>30</thirtymin>     </averagingperiod>     <timeweighting name="timeweighting">         <fast>fast</fast>         <slow>slow</slow>     </timeweighting>     <reboottime name="reboottime">         <midnight>midnight</midnight>         <sevenam>7am</sevenam>         <sevenpm>7pm</sevenpm>         <elevenpm>23pm</elevenpm>     </reboottime>     <remoteupload name="remoteupload">         <nointernet>nointernet</nointernet>         <vodafone>vodafone</vodafone>     </remoteupload> </configuration>""" d = xmltodict.parse(xml) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -