I have a simple XML file and need to add <item>
Tried it with ElementTree but got lost...
So - from this :
<channel>
<title>Latest Critical Tickets</title>
<channelName>Critical </channelName>
<item>
<title>Ticket number 01</title>
<link>https://blablabla.com/01</link>
<pubDate>Wed, 14 Jul 2021 16:45:00 GMT</pubDate>
</item>
</channel>
To this :
<channel>
<title>Latest Critical Tickets</title>
<channelName>Critical </channelName>
<item>
<title>Ticket number 01</title>
<link>https://blablabla.com/01</link>
<pubDate>Wed, 14 Jul 2021 16:45:00 GMT</pubDate>
</item>
<item>
<title>Ticket number 02</title>
<link>https://blabl12abla.com/01</link>
<pubDate>Wed, 16 Jul 2021 21:13:00 GMT</pubDate>
</item>
</channel>
thanks in advnaced!
I think this should do what you need:
import xml.etree.ElementTree as ET
from xml.dom import minidom
root = ET.fromstring("""<channel>
<title>Latest Critical Tickets</title>
<channelName>Critical </channelName>
<item>
<title>Ticket number 01</title>
<link>https://blablabla.com/01</link>
<pubDate>Wed, 14 Jul 2021 16:45:00 GMT</pubDate>
</item>
</channel>""")
element_to_add = ET.fromstring("""<item>
<title>Ticket number 02</title>
<link>https://blabl12abla.com/01</link>
<pubDate>Wed, 16 Jul 2021 21:13:00 GMT</pubDate>
</item>""")
root.insert(-1, element_to_add)
print(minidom.parseString(ET.tostring(root)).toprettyxml(indent=" "))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.