简体   繁体   English

在 python 中解析 XML 文件并检索嵌套的子项

[英]Parse XML file in python and retrieve nested children

This post has everything of a duplicate.. and yet I can not figure it out despite the dozen of posts I've read.这篇文章的所有内容都是重复的。尽管我读过十几篇文章,但我还是无法弄清楚。

Here is the XML (a short version):这是 XML(简短版本):

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
<coordinates xmlns="http://www.egi.com/coordinates_mff" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <sensorLayout>
        <name>HydroCel GSN 256 1.0</name>
        <sensors>
            <sensor>
                <name></name>
                <number>1</number>
                <type>0</type>
                <x>6.962</x>
                <y>5.382</y>
                <z>-2.191</z>
            </sensor>
            <sensor>
                <name></name>
                <number>2</number>
                <type>0</type>
                <x>6.484</x>
                <y>6.404</y>
                <z>-0.140</z>
            </sensor>
            <sensor>
                <name></name>
                <number>3</number>
                <type>0</type>
                <x>5.699</x>
                <y>7.208</y>
                <z>1.791</z>
            </sensor>
        </sensors>
    </sensorLayout>
    <acqTime>2006-04-28T15:32:00.000000-08:00</acqTime>
    <acqMethod>An Average of Many Data Sets&#x09;&#x09;&#x09;&#x09;</acqMethod>
    <defaultSubject>true</defaultSubject>
</coordinates>

How can I retrieve a list of sensors with the name, number, and coordinates for each sensor?如何检索包含每个传感器的名称、编号和坐标的传感器列表?

I am struggling to iterate over this tree:我正在努力迭代这棵树:

tree = ET.parse(xml_fname)
root = tree.getroot()

# And then what?
# root.iter("sensor")  does not yield any element
# root.find("sensor")  returns None
# and so on...

The tag of the root looks weird to me..根的标签对我来说看起来很奇怪..

root.tag
'{http://www.egi.com/coordinates_mff}coordinates'

Thanks for the help!谢谢您的帮助!

You can just parse it with namespace support.您可以使用命名空间支持对其进行解析。 See below.见下文。

from xml.etree import ElementTree as ET
root = ET.parse(xml_fname)
# Use an XPath to get all sensor tags
sensors = root.findall(".//sensorLayout/sensors/sensor", namespaces={"": "http://www.egi.com/coordinates_mff"})
# Burst values from each child of every sensor
sensor_values = [{ct.tag.split("}")[-1]: ct.text for ct in sensor.getchildren()} for sensor in sensors]
# Dict key is formed by removing the namspace part in the tag - dirty!
print(sensor_values)

You get something lik e你会得到类似的东西

[
{'name': None, 'number': '1', 'type': '0', 'x': '6.962', 'y': '5.382', 'z': '-2.191'}, 
{'name': None, 'number': '2', 'type': '0', 'x': '6.484', 'y': '6.404', 'z': '-0.140'}, 
{'name': None, 'number': '3', 'type': '0', 'x': '5.699', 'y': '7.208', 'z': '1.791'}
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM