How do I append new data to existing XML using Python ElementTree?

雨燕双飞 提交于 2019-11-30 22:58:59
naveen tamanam

In ElementTree, Element objects have an "append" method. By using this method you can directly add the new XML tag.

For example:

user = Element('user')
user.append((Element.fromstring('<user username="admin" fullname="admin" password="xx"  uid="1000"/>')))

where "Element" comes from from xml.etree.ElementTree import Element.

After getting the XML root, convert the children of that root it to string - "ET.toString()", and ".split()" it into pieces, so that you can create a list, and append the new line to that list. And then use ".join()" to create a string from the list. After that, use "ET.fromString()" method to create a new xml. And write it to the file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!