如何使用Minidom从Python中的XML文件中提取一系列元素

发布于 2025-01-23 15:30:36 字数 944 浏览 2 评论 0原文

我有一个如下的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文