将带有元素树的元素插入现有 xml
insert element with element tree to existing xml
我正在尝试找到使用元素树向这些项目条目添加元素的最简单方法。
我将以下 XML 输出存储在 (xmldata) 中。我还不想将其写入文件,我只需要添加 ID,以便通过将其与其他数据中的相应 ID 相关联来进一步使用数据。
你看到的地方
<archived type="bool">False</archived>
我想在上面添加
<id>555666</id>
列表中的所有项目(所有项目都使用相同的 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)
不确定如何完成此操作,所以我有更新的数据要使用。
像这样的东西应该会给你灵感
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")
我正在尝试找到使用元素树向这些项目条目添加元素的最简单方法。
我将以下 XML 输出存储在 (xmldata) 中。我还不想将其写入文件,我只需要添加 ID,以便通过将其与其他数据中的相应 ID 相关联来进一步使用数据。
你看到的地方
<archived type="bool">False</archived>
我想在上面添加
<id>555666</id>
列表中的所有项目(所有项目都使用相同的 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)
不确定如何完成此操作,所以我有更新的数据要使用。
像这样的东西应该会给你灵感
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")