0

I am trying to parse an XML file to a list with Python. I have looked at some solutions on this site and others and could not make them work for me. I have managed to do it but in a laborious way that seems stupid to me. It seems that there should be an easier way.

I have tried to adapt other peoples code to suit my needs but that is not working as I am not always sure of what I am reading.

This is the 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 this is the Python program.

#!/usr/bin/python
import xml.etree.ElementTree as ET


import os
try:
    import cElementTree as ET
except ImportError:
    try:
        import xml.etree.cElementTree as ET
    except ImportError:
        exit_err("Failed to import cElementTree from any 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')

for i in location_settings:

    latitude = i.find('latitude').text
    latHemi = i.find('latHemi').text
    longitude = i.find('longitude').text
    longHemi = i.find('longHemi').text


for i in mic_settings:
    sensitivity = i.find('sensitivity').text

for i in weighting:
    cWeight = i.find('cWeight').text
    aWeight = i.find('aWeight').text

for i 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

for i 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

for i in time_weight:
    fast = i.find('fast').text
    slow = i.find('slow').text

for i in reboot:
    midnight = i.find('midnight').text
    sevenAm = i.find('sevenAm').text
    sevenPm = i.find('sevenPm').text
    elevenPm= i.find('elevenPm').text

for i 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)
2
  • Welcome to StackOverflow. Can you please describe your expected output that you want to parse from the XML? You may edit your questioin to include that Commented Jul 25, 2016 at 19:16
  • What sort of list are you expecting? A single flat list of the toplevel elements? A list of lists? Commented Jul 25, 2016 at 19:37

2 Answers 2

2

The problem you're posing isn't very well defined. The XML structure doesn't conform very well to a list structure to begin with. If you're new to python, I think the best way to go about what you're trying to do is to use something like xmltodict which will parse the implicit schema in your xml to 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)
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the comments. Sorry if the question was not well posed. I have found an answer myself. I was looking to parse the XML child elements into a list for later use in another program. I figured it out. Thank you for your patience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.