在现有的XML文件中,以Python在特定但未定义的位置后添加一个新的XML元素。

huangapple go评论70阅读模式
英文:

Adding a new XML Element after a specific but not defined position in an exisiting XML File in python

问题

I want to add a new XML Element to an existing XML File. The problem is that the Position varies from file to file but I want to insert my new Element always behind the last Tag of a specific element. So let's say I have X amount of <Data> Tags in the file and after the last <Data> Tag I want to create and insert a new XML Element with an <Example> Tag.

Here is my code so far:

import xml.etree.ElementTree as ET

xml_file = "example.xml";

tree = ET.parse(xml_file)
root = tree.getroot()

LastDataTag = root.findall('.//Data')[-1]

new_element = ET.Element('Example')

root.insert(LastDataTag + 1, new_element)

The Problem I see is that I don't get an integer as a return from findall, but rather a Position within the file like this <Element 'Data' at 0x000001E99BE7A610>. I get that this is obviously not working because python can't add +1 to this since it's not an integer.

Is there a way to translate this to an integer so you can work with it this way, or is there another way of doing this or getting the index of the last <Data> Tag? The other posts I looked at did it the same way with the insert method, but they all had a fixed index number that did not change.

英文:

I want to add a new XML Element to an existing XML File. The problem is that the Position varies from file to file but I want to insert my new Element always behind the last Tag of a specific element. So let's say I have X amount of &lt;Data&gt; Tags in the file and after the last &lt;Data&gt; Tag I want to create and insert a new XML Element with a &lt;Example&gt; Tag.

Here is my code so far:

import xml.etree.ElementTree as ET


xml_file = &quot;example.xml&quot;


tree = ET.parse(xml_file)
root = tree.getroot()

LastDataTag = root.findall(&#39;.//Data&#39;)[-1]

new_element = ET.Element(&#39;Example&#39;)

root.insert(LastDataTag + 1, new_element)

The Problem I see is that I don't get an integer as a return form findall, but rather a Postion within the file like this &lt;Element &#39;Data&#39; at 0x000001E99BE7A610&gt; I get that this is obviously not working because python cant add +1 to this since it's not an integer.

Is there a way to translate this to an integer so you can work with it this way or is there another way of doing this or getting the index of the last &lt;Data&gt; Tag? The other posts I looked at did it the same way with the insert method but they all had a fixed index number that did not change.

答案1

得分: 0

If you're able to use lxml instead of ElementTree, it has the method addnext() that makes it much easier to add a following sibling...

如果您可以使用lxml而不是ElementTree,它具有addnext()方法,可以更轻松地添加后续同级元素...

If you're stuck with ElementTree, you'll first have to find the parent of the Data element so you can determine its index. Then you can use .insert() with the index plus one...

如果您使用ElementTree,首先必须找到Data元素的父元素,以确定其索引。然后,您可以使用索引加一的方式来使用.insert()插入元素...

英文:

If you're able to use lxml instead of ElementTree, it has the method addnext() that makes it much easier to add a following sibling...

from lxml import etree

xml_file = &#39;example_input.xml&#39;
tree = etree.parse(xml_file)

last_data_elem = tree.findall(&#39;.//Data&#39;)[-1]
new_element = etree.Element(&#39;Example&#39;)
last_data_elem.addnext(new_element)

tree.write(&#39;example_output.xml&#39;)

If you're stuck with ElementTree, you'll first have to find the parent of the Data element so you can determine its index. Then you can use .insert() with the index plus one...

import xml.etree.ElementTree as ET

xml_file = &#39;example_input.xml&#39;

tree = ET.parse(xml_file)
root = tree.getroot()

last_data_elem = root.findall(&#39;.//Data&#39;)[-1]
new_element = ET.Element(&#39;Example&#39;)

for data_parent in root.findall(&#39;.//*[Data]&#39;):
    for data_elem in data_parent.findall(&#39;Data&#39;):
        if data_elem is last_data_elem:
            data_index = list(data_parent).index(data_elem)
            data_parent.insert(data_index + 1, new_element)

tree.write(&#39;example_output.xml&#39;)

huangapple
  • 本文由 发表于 2023年4月11日 03:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定