如何将XML文件解析为列表?

时间:2016-07-25 19:13:22

标签: python xml

我正在尝试使用Python将XML文件解析为列表。我已经在本网站和其他网站上查看了一些解决方案,但无法让它们为我工作。我设法做到了,但是以一种对我来说似乎很愚蠢的艰苦方式。似乎应该有一种更简单的方法。

我已经尝试调整其他人的代码以满足我的需求,但这不起作用,因为我并不总是确定我正在阅读的内容。

这是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>

这是Python程序。

#!/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 个答案:

答案 0 :(得分:2)

你提出的问题没有明确定义。 XML结构与列表结构不一致。如果你是python的新手,我认为你要做的最好的方法是使用类似xmltodict的东西来解析你的xml中的隐式模式到python数据结构。

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)

答案 1 :(得分:0)

感谢您的评论。对不起,如果问题没有得到很好的解决。我自己找到了答案。我希望将XML子元素解析为一个列表,以便以后在另一个程序中使用。我想到了。感谢您的耐心等待。