import xml.etree.ElementTree as xml
root = xml.Element('XmlToTs')
Eit = xml.Element('Eit')
root.append(Eit)
xml.SubElement(Eit, "Pid").text = "18"
Event = xml.Element("Event")
xml.SubElement(Event, "EventId").text = "12677"
Short = xml.SubElement(Event, "ShortEventDescriptor")
xml.SubElement(Short, "Iso639LanguageCode").text = "deu"
xml.SubElement(Short, "EventName").text = "Talk am Teich"
xml.SubElement(Short, "Text").text = "Heute wird mit den Enten getalked"
EID=12500
Eit.append(Event)
Eit.append(Event)
Eit.append(Event)
for EventId in Eit.iter('EventId'):
EventId.text = str(EID)
EID=EID+1
我希望有多个具有默认子项的“事件”节点。之后我想迭代所有并修改他们的子文本。
但是目前iter循环为所有EventIds提供12502
谢谢克里斯蒂安
答案 0 :(得分:1)
当您致电Event = xml.Element("Event")
时,您可以创建Event元素的一个实例。
通过将此实例传递给.append()
方法并调用它(在您的情况下为三次),ElementTree会附加三个EventId-Elments,但实际上内存中只有一个 EventId-Element。
你可以用以下几行来解决这个问题:
import xml.etree.ElementTree as xml
root = xml.Element('XmlToTs')
Eit = xml.Element('Eit')
root.append(Eit)
xml.SubElement(Eit, "Pid").text = "18"
Event = xml.Element("Event")
xml.SubElement(Event, "EventId").text = "12677"
Short = xml.SubElement(Event, "ShortEventDescriptor")
xml.SubElement(Short, "Iso639LanguageCode").text = "deu"
xml.SubElement(Short, "EventName").text = "Talk am Teich"
xml.SubElement(Short, "Text").text = "Heute wird mit den Enten getalked"
Eit.append(Event)
Eit.append(Event)
Eit.append(Event)
for EventId in Eit.iter('EventId'):
print(EventId)
输出是这样的:
<ELment 'EventId' at 0x17ee170>
<ELment 'EventId' at 0x17ee170>
<ELment 'EventId' at 0x17ee170>
如您所见所有 EventId-Elements都有相同的内存加法器!
当您使用.iter()
方法迭代所有EventId-Tags时,您可以在XML代码中找到ElmentTree,在每次迭代中您都要访问相同的对象,即相同 EventId-Tag。
这就是为什么每个EventId-Element的所有文本属性都被分配给相同的数字12502的原因; 12500增加了两倍。
简而言之,您需要创建多个EventId实例;例如在for循环中:
for _ in range(3):
Event = xml.Element("Event")
xml.SubElement(Event, "EventId").text = "12677"
Short = xml.SubElement(Event, "ShortEventDescriptor")
xml.SubElement(Short, "Iso639LanguageCode").text = "deu"
xml.SubElement(Short, "EventName").text = "Talk am Teich"
xml.SubElement(Short, "Text").text = "Heute wird mit den Enten getalked"
Eit.append(Event)
之后你可以运行你的代码......
EID=12500
for EventId in Eit.iter('EventId'):
EventId.text= str(EID)
EID += 1
......它完美无缺! :)
完整的源代码:
import xml.etree.ElementTree as xml
from xml.dom import minidom
root = xml.Element('XmlToTs')
Eit = xml.Element('Eit')
root.append(Eit)
xml.SubElement(Eit, "Pid").text = "18"
for _ in range(3):
Event = xml.Element("Event")
xml.SubElement(Event, "EventId").text = "12677"
Short = xml.SubElement(Event, "ShortEventDescriptor")
xml.SubElement(Short, "Iso639LanguageCode").text = "deu"
xml.SubElement(Short, "EventName").text = "Talk am Teich"
xml.SubElement(Short, "Text").text = "Heute wird mit den Enten getalked"
Eit.append(Event)
EID=12500
for EventId in Eit.iter('EventId'):
EventId.text= str(EID)
EID += 1
#print the generated XML code
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = xml.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
print(prettify(Eit))