如何使用Minidom从Python中的XML文件中提取一系列元素
我有一个如下的XML文件,我想将元素提取在第23个到第35个时间步段元素之间,并使用Minidom Python将它们放入数组中:
<xml>
<fcd>
<?xml version="1.0" encoding="UTF-8"?>
<fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
<timestep time="0.00"/>
<timestep time="1.00">
<vehicle id="myflow.0" x="5.10" y="998.40" angle="90.00" type="car" speed="14.25" pos="5.10" lane="e18_2" slope="0.00" acceleration="0.00"/>
</timestep>
<timestep time="2.00">
</timestep>
<timestep time="3.00">
...
我尝试的是以下内容,但我认为这不是一个好方法。
tt=doc.getElementsByTagName("timestep")
for time in tt:
if(float(time.getAttribute("time"))>23.00 and float(time.getAttribute("time"))<35.00):
vehicles=time.getElementsByTagName("vehicle")
I have a xml file like the following, and I want to extract the elements between 23th until 35th timestep elements and put them into an array using minidom python:
<xml>
<fcd>
<?xml version="1.0" encoding="UTF-8"?>
<fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
<timestep time="0.00"/>
<timestep time="1.00">
<vehicle id="myflow.0" x="5.10" y="998.40" angle="90.00" type="car" speed="14.25" pos="5.10" lane="e18_2" slope="0.00" acceleration="0.00"/>
</timestep>
<timestep time="2.00">
</timestep>
<timestep time="3.00">
...
what I tried is the following but I think its not a good approach.
tt=doc.getElementsByTagName("timestep")
for time in tt:
if(float(time.getAttribute("time"))>23.00 and float(time.getAttribute("time"))<35.00):
vehicles=time.getElementsByTagName("vehicle")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论