Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解析XML以获取节点的子名称_Python_Xml_Python 3.x_Python 2.7_Xml Parsing - Fatal编程技术网

Python 解析XML以获取节点的子名称

Python 解析XML以获取节点的子名称,python,xml,python-3.x,python-2.7,xml-parsing,Python,Xml,Python 3.x,Python 2.7,Xml Parsing,我是Python/XML新手。我想解析一个XML以获取父节点下的数据。 我的XML有一组名称不同的“实体”节点。在每个“实体”下都有一组“属性”,如下所示。我需要获取特定实体的所有属性。例如,如果它是实体“Quit”,我需要将相应的“Property”名称作为字符串用逗号分隔;比如,{QuitId,QuitReason,Status,QuitTime}使用Python脚本。我只需要属性的“Name”属性 <Entity Name="Boot"> <Property Name

我是Python/XML新手。我想解析一个XML以获取父节点下的数据。 我的XML有一组名称不同的“实体”节点。在每个“实体”下都有一组“属性”,如下所示。我需要获取特定实体的所有属性。例如,如果它是实体“Quit”,我需要将相应的“Property”名称作为字符串用逗号分隔;比如,{QuitId,QuitReason,Status,QuitTime}使用Python脚本。我只需要属性的“Name”属性

<Entity Name="Boot">
  <Property Name="BootId" Type="Edm.Int32" Nullable="false"/>
  <Property Name="Name" Type="Edm.String" Nullable="false"/>
  <Property Name="Status" Type="Edm.String" Nullable="false"/>
  <Property Name="BootTime" Type="Edm.DateTimeOffset"/>
</Entity>
<Entity Name="Quit">
  <Property Name="QuitId" Type="Edm.Int32" Nullable="false"/>
  <Property Name="QuitReason" Type="Edm.String" Nullable="false"/>
  <Property Name="Status" Type="Edm.String" Nullable="false"/>
  <Property Name="QuitTime" Type="Edm.DateTimeOffset"/>
</Entity>

我可以得到所需的名字。谢谢你的指点

import xml.etree.ElementTree as ET
tree = ET.parse('D:\Code\XM_metadata.xml')
root = tree.getroot()
properties = ''
for prop in root.findall(".//*[@Name='Quit']/{http://docs.oasis-open.org/odata/ns/edm}Property"):
    properties = properties + prop.get('Name') + ','
properties = properties[:-1]
print (properties)

请发布根标记(XML的要求)和尝试的Python代码。您应该阅读本教程,其中介绍了基础知识以及如何从简单的XML结构中提取数据:谢谢您的评论。我可以使用添加到问题中的代码获取名称。但是,我不知道如何获得特定实体的“属性”
import xml.etree.ElementTree as ET
tree = ET.parse('D:\Code\XM_metadata.xml')
root = tree.getroot()
properties = ''
for prop in root.findall(".//*[@Name='Quit']/{http://docs.oasis-open.org/odata/ns/edm}Property"):
    properties = properties + prop.get('Name') + ','
properties = properties[:-1]
print (properties)