Python 使用元素树解析XML文件
我想解析一个XML文件,以便获得作为变量的信息,以便进一步研究 XML的一部分如下所示:Python 使用元素树解析XML文件,python,xml,parsing,Python,Xml,Parsing,我想解析一个XML文件,以便获得作为变量的信息,以便进一步研究 XML的一部分如下所示: '''<SchedulingPeriod ID="sprint01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="competition.xsd"> <StartDate>2010-01-01&l
'''<SchedulingPeriod ID="sprint01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="competition.xsd">
<StartDate>2010-01-01</StartDate>
<EndDate>2010-01-28</EndDate>
<Skills>
<Skill>Nurse</Skill>
</Skills>
<ShiftTypes>
<Shift ID="E">
<StartTime>06:30:00</StartTime>
<EndTime>14:30:00</EndTime>
<Description>Early</Description>
<Skills>
<Skill>Nurse</Skill>
</Skills>
</Shift>
'''
谢谢,如果您对如何存储这些元素有任何建议,我们将不胜感激。
Arthur使用以下代码提取ID属性:
import xml.etree.ElementTree as ET
tree = ET.parse('a.xml')
root = tree.getroot()
for child in root:
if child.tag == 'ShiftTypes':
for i in child:
print ('Here is the ID: ', i.attrib)
for j in i:
if j.tag == 'StartTime':
print ('Here is StartTime:', j.text)
elif j.tag == 'EndTime':
print ('Here is EndTime:', j.text)
elif j.tag == 'Description':
print ('Here is Description:', j.text)
以下是有关解析XML数据的有用教程:
import xml.etree.ElementTree as ET
tree = ET.parse('a.xml')
root = tree.getroot()
for child in root:
if child.tag == 'ShiftTypes':
for i in child:
print ('Here is the ID: ', i.attrib)
for j in i:
if j.tag == 'StartTime':
print ('Here is StartTime:', j.text)
elif j.tag == 'EndTime':
print ('Here is EndTime:', j.text)
elif j.tag == 'Description':
print ('Here is Description:', j.text)
Here is the ID: {'ID': 'E'}
Here is StartTime: 06:30:00
Here is EndTime: 14:30:00
Here is Description: Early