0
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:
    username             = child.attrib['username']
    password             = child.attrib['password']
    grant_admin_rights   = child.attrib['grant_admin_rights']
    create_private_share = child.attrib['create_private_share']
    uid                  = child.attrib['uid']



root  = ET.Element("users")
user  = ET.SubElement(root, "user")
user.set("username",username)
user.set("password",password)
user.set("grant_admin_rights",grant_admin_rights)
user.set("create_private_share",create_private_share)
user.set("uid",uid)

tree = ET.ElementTree(root)
myxml = tree.write("new.xml")

output of this code :-

<users><user create_private_share="no" grant_admin_rights="no" password="sp" uid
="1000" username="us" /></users>

But am trying to make it like this :-

<users>

<user create_private_share="no" grant_admin_rights="no" password="sp" uid
="1000" username="us" ><group>hfhfhf</group> </user>

</users>

instead of this <user />, am trying <user> <group>fgfg</group> </user> . Thanks

1 Answer 1

2

Try the SubElement factory function:

group = SubElement(user, "group")
# ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.