1

I m struggling adding data to a subtree using ElementTree; my XML file looks like this: enter image description here

Generally, I would use et.SubElement to add a new tag, but it will it write under "file" (i.e. = et.SubElement(tree.getroot(), 'new tag') and then I would add what I need using .text .attrib . etc etc ).

I would like to add another "instance" to the path: file/all_instances, which is the bit that I am struggling with.

In conclusion, there should be another "instance" under the subtree "all_instance", with the same structure (ID, Start, etc etc). So, my ideal output would be:

<instance>
    <ID> .. </ID>
    <start> ..</start>
    <end>.. </end>
    <code> ..</code>
</instance>

Thank you for your help :)

EDIT:

Here my XML file:

<file>
<SESSION_INFO>
<start_time>2016-11-24 02:58:34.36 -0800</start_time>
</SESSION_INFO>
<ALL_INSTANCES>
<instance>
<ID>1</ID>
<start>18.8426378227</start>
<end>71.6020237264</end>
<code>Shot </code>
</instance>
<instance>
<ID>2</ID>
<start>139.4355198883</start>
<end>199.7319609211</end>
<code>Shot </code>
<label>
<text>Succ</text>
</label>
</instance>
<instance>
<ID>3</ID>
<start>237.4172365666</start>
<end>305.2507327285</end>
<code>Shot </code>
</instance>


</ALL_INSTANCES>

<ROWS>
<row>
<code>Shot </code>
<R>57000</R>
<G>57000</G>
<B>57000</B>
</row>
<row>
<code>Shot Succ</code>
<R>57000</R>
<G>57000</G>
<B>57000</B>
</row>
</ROWS>
</file>
3
  • 1
    What exactly are you struggling with? It is not clear. Show us code and XML markup: please provide a minimal reproducible example. Commented Nov 21, 2019 at 13:05
  • 1
    Did my answer help or are you still having issues? Commented Nov 22, 2019 at 20:04
  • 1
    Yes man , it works! Sorry for late reply, I missed this :) Commented Dec 17, 2019 at 14:03

1 Answer 1

1

Instead of using:

et.SubElement(tree.getroot(), 'instance')

you could use:

et.SubElement(tree.find("./ALL_INSTANCES"), 'instance')

You could also build up your new instance element structure as a string first then turn it into an Element and either append() or insert() it into ALL_INSTANCES.

Example...

import xml.etree.ElementTree as ET

tree = ET.parse("input.xml")

new_instance = """<instance>
<ID> .. </ID>
<start> .. </start>
<end> .. </end>
<code> .. </code>
</instance>
"""

tree.find("./ALL_INSTANCES").append(ET.fromstring(new_instance))

print(ET.tostring(tree.getroot()).decode())

prints...

<file>
<SESSION_INFO>
<start_time>2016-11-24 02:58:34.36 -0800</start_time>
</SESSION_INFO>
<ALL_INSTANCES>
<instance>
<ID>1</ID>
<start>18.8426378227</start>
<end>71.6020237264</end>
<code>Shot </code>
</instance>
<instance>
<ID>2</ID>
<start>139.4355198883</start>
<end>199.7319609211</end>
<code>Shot </code>
<label>
<text>Succ</text>
</label>
</instance>
<instance>
<ID>3</ID>
<start>237.4172365666</start>
<end>305.2507327285</end>
<code>Shot </code>
</instance>
<instance>
<ID> .. </ID>
<start> .. </start>
<end> .. </end>
<code> .. </code>
</instance></ALL_INSTANCES>
<ROWS>
<row>
<code>Shot </code>
<R>57000</R>
<G>57000</G>
<B>57000</B>
</row>
<row>
<code>Shot Succ</code>
<R>57000</R>
<G>57000</G>
<B>57000</B>
</row>
</ROWS>
</file>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.