将具有元素树的元素插入现有xm

2024-05-14 21:31:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到最简单的方法,使用元素树将元素添加到这些条目中。在

我将下面的XML输出存储在(xmldata)中。我还不想把这个写到一个文件中,我只需要添加id,这样我就可以通过将它与其他数据中的相应id相关联来进一步使用数据。在

你看到的地方

     <archived type="bool">False</archived>

我想补充一下

^{pr2}$

到列表中的所有项(所有项的id相同)

        <?xml version="1.0" encoding="UTF-8" ?>
            <root>
                <tasks type="list">
                    <item type="dict">
                        <archived type="bool">False</archived>
                        <budget_spent type="float">0.0</budget_spent>
                        <billable_hours type="float">0.0</billable_hours>
                        <billable type="bool">True</billable>
                        <billable_amount type="float">0.0</billable_amount>
                        <budget_left type="null"/>
                        <over_budget_percentage type="null"/>
                        <task_id type="int">6356</task_id>
                        <detailed_report_url type="str">/reports/detailed/</detailed_report_url>
                        <name type="str">Planning</name>
                        <internal_cost type="float">0.0</internal_cost>
                        <budget type="null"/>
                        <budget_spent_percentage type="null"/>
                        <total_hours type="float">0.0</total_hours>
                        <over_budget type="null"/>
                        <billed_rate type="float">0.0</billed_rate>
                    </item>
                    <item type="dict">
                        <archived type="bool">False</archived>
                        <budget_spent type="float">0.0</budget_spent>
                        <billable_hours type="float">0.0</billable_hours>
                        <billable type="bool">True</billable>
                        <billable_amount type="float">0.0</billable_amount>
                        <budget_left type="null"/>
                        <over_budget_percentage type="null"/>
                        <task_id type="int">6357</task_id>
                        <detailed_report_url type="str">/detailed/123</detailed_report_url>
                        <name type="str">Planning</name>
                        <internal_cost type="float">0.0</internal_cost>
                        <budget type="null"/>
                        <budget_spent_percentage type="null"/>
                        <total_hours type="float">0.0</total_hours>
                        <over_budget type="null"/>
                        <billed_rate type="float">0.0</billed_rate>
                    </item>
                </tasks>

****更新****

根据DAXaholic的回答,我添加了以下内容:

     tree = ET.fromstring(xmldata)
     for item in tree.iterfind('tasks/item'):
      idtag = ET.Element('id')
      idtag.text = '555666'
      item.insert(0, idtag)

不知道如何完成这一切,所以我有更新的数据使用。在


Tags: idtypefloatitemamountnulloverbool
1条回答
网友
1楼 · 发布于 2024-05-14 21:31:40

像这样的东西应该能让你明白

root = ET.fromstring(xmldata)
for item in root.iterfind('tasks/item'):
    idtag = ET.Element('id')
    idtag.text = '555666'
    item.insert(0, idtag)
xmldata = ET.tostring(root, encoding="unicode")

相关问题 更多 >

    热门问题