【发布时间】:2020-12-05 11:26:00
【问题描述】:
我正在尝试为一个项目读取一些 xml 数据,但它不起作用...我有这个代码(使用的 xml 文件如下所示):
import time
from xml.etree.ElementTree import fromstring, ElementTree
import xml.etree.ElementTree as ET
ET.register_namespace('', "http://www.w3.org/2001/XMLSchema-instance")
ET.register_namespace('', "http://bison.connekt.nl/tmi8/kv6/msg")
while True:
print("--------------------------------------------")
tree = ET.parse("RET.xml")
root = tree.getroot()
print(root)
for debug in root.findall(".//"):
print(debug.text)
for line in root.findall('.Version'):
print(line.text)
print("--------------------------------------------")
time.sleep(5)
它成功地找到了所有元素的内容,但是当我搜索像“版本”这样的特定元素时,它不会返回任何内容。这是当前的输出:
<Element '{http://bison.connekt.nl/tmi8/kv6/msg}VV_TM_PUSH' at 0x03D775A0>
RET
BISON 8.1.1.0
KV6posinfo
2020-12-04T21:22:56.1275145+01:00
ttt
RET
M007
2020-12-04
200180
0
HA8215
0
2020-12-04T21:22:56.1119143+01:00
SERVER
0
-920
--------------------------------------------
这是使用的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<VV_TM_PUSH xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://bison.connekt.nl/tmi8/kv6/msg">
<SubscriberID>
RET</SubscriberID><Version>
BISON 8.1.1.0</Version>
<DossierName>KV6posinfo</DossierName>
<Timestamp>2020-12-04T21:22:56.1275145+01:00</Timestamp>
<KV6posinfo>ttt
<ONSTOP>
<dataownercode>RET</dataownercode>
<lineplanningnumber>M007</lineplanningnumber>
<operatingday>2020-12-04</operatingday>
<journeynumber>200180</journeynumber>
<reinforcementnumber>0</reinforcementnumber>
<userstopcode>HA8215</userstopcode>
<passagesequencenumber>0</passagesequencenumber>
<timestamp>2020-12-04T21:22:56.1119143+01:00</timestamp>
<source>SERVER</source>
<vehiclenumber>0</vehiclenumber>
<punctuality>-920</punctuality>
</ONSTOP>
</KV6posinfo>
</VV_TM_PUSH>
出于测试目的,我在标签中添加了“ttt”。
谁能帮忙?
【问题讨论】:
标签: python xml-parsing elementtree