I like to create a nodes every time when i go through a loop.But presently only the last value of the loop will be used. How can i achieve this using python. following is my example.
My xml :-
<person>
<user name="david" password="super"></user>
<user name="alen" password="boss"></user>
<user name="windeesal" password="sp"></user>
</person>
The python code:
import xml.etree.ElementTree as ET
doc = ET.parse("users.xml")
root = doc.getroot() #Returns the root element for this tree.
root.keys() #Returns the elements attribute names as a list. The names are returned in an arbitrary order
for child in root:
name = child.attrib['name']
password = child.attrib['password']
root = ET.Element("person")
user = ET.SubElement(root, "user")
user.set("username",username)
user.set("password",password)
tree = ET.ElementTree(root)
myxml = tree.write("new.xml")
print myxml
Out put of the code contain only last value of loop 🙁
<person>
<user password="sp" username="windeesal" />
</person>
how to create the nodes every time i go through the loop then join the results and write them to the file .? am really a beginner please give me a detail explanation. Thank you very much .
Try next one. Your understanding of python seems to be very basic so I am not sure what to write about problem.
Please ask if you need explanation! 🙂