Carver Stone
Carver Stone

Reputation: 105

insert element with element tree to existing xml

I'm trying to find the simplest way to add an element to these item entries using element tree.

I have the below XML output stored in (xmldata). I don't want to write this to a file yet, I just need to add the id so I an further use the data by relating it to a corresponding id in other data.

Where you see

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

Just above that I want to add

     <id>555666</id>

to all items in the list (same id to all)

        <?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>

**** update ****

Based on the answer from DAXaholic I've added this:

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

not sure how to finish this off so I have the updated data to use.

Upvotes: 1

Views: 785

Answers (1)

DAXaholic
DAXaholic

Reputation: 35348

Something like this should give you the idea

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")

Upvotes: 1

Related Questions