Short version / Updates & Edits:
The only thing you need to do is (A) add newsub1
and newsub2
to new
. And (B) add new
to root
.
root = ET.fromstring(xml) # or whichever way you're getting `root`
# these 3 lines form your code:
new = ET.Element('vehicleTravelTimeMeasurement', name="kkk", no="4")
newsub1 = ET.Element('start', link='1', pos='3.88888')
newsub2 = ET.Element('end', link='3', pos='3.88888')
# the next steps to add
new.append(newsub1)
new.append(newsub2)
root.append(new)
Note that (A) & (B) can be done in any order and this can be shortened, as below:
>>> root = ET.fromstring(xml)
>>> new = ET.Element('vehicleTravelTimeMeasurement', name="kkk", no="4")
>>> root.append(new) # note that I've immediately added `new`
>>> ET.SubElement(new, 'start', link='1', pos='3.88888')
<Element 'start' at 0x24707b8>
>>> ET.SubElement(new, 'end', link='3', pos='3.88888')
<Element 'end' at 0x24ea978>
>>> # there's no need to store the subelements in `newsub1` and
... # `newsub2` if you don't need to do anything with them
...
>>> indent(root)
>>> print ET.tostring(root)
<vehicleTravelTimeMeasurements>
<vehicleTravelTimeMeasurement name="ckkkkkkkkkk" no="2">
<start link="1" pos="3.864983" />
<end link="3" pos="23.275375" />
</vehicleTravelTimeMeasurement>
<vehicleTravelTimeMeasurement name="" no="3">
<start link="1" pos="3.864983" />
<end link="2" pos="13.275375" />
</vehicleTravelTimeMeasurement>
<vehicleTravelTimeMeasurement name="kkk" no="4">
<start link="1" pos="3.88888" />
<end link="3" pos="3.88888" />
</vehicleTravelTimeMeasurement>
</vehicleTravelTimeMeasurements>
Notes:
- I've added
new
to root
just after creating it.
- Use
append
instead of insert
if you know you're always appending, instead of needing to keep track of the no
's in your original xml
- unless you need to read that anyway to calculate the next '
no
' attribute
ET.SubElement(new)
updates new
(and root
) even though new
has already been appended.
- There's no need to store the subelements in
newsub1
and newsub2
if you don't need to do anything with them.
- First method (like yours) creates the elements and then adds them to root or new.
- Second method uses
ET.SubElement(new, ...)
to add the elements to its parent.
- The function
indent
is from here, which quotes this source.
Re # 4.2 above, could also be done as:
root = ET.fromstring(xml)
new = ET.SubElement(root, 'vehicleTravelTimeMeasurement', name="kkk", no="4")
# `new` is already created as a subelement of `root` and appended
ET.SubElement(new, 'start', link='1', pos='3.88888')
ET.SubElement(new, 'end', link='3', pos='3.88888')
From the Subelement docs:
This function creates an element instance, and appends it to an existing element.
(emphasis mine)