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")
print myxml
私の入力xml:-
<users>
<user username="admin" fullname="admin" password="admin" create_private_share="yes" remote_access="yes" grant_admin_rights="yes" enable_encrypt_share="yes" volume="DataVolume5" uid="1000"></user>
<user username="os" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
<user username="us" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
</users>
このコードの出力:
<user create_private_share="no" grant_admin_rights="no" password="sp" uid="1000" username="us" />
入力 xml として正確な xml を取得しようとしていますが、現在、すべての子を反復していますが、出力のみが来ています。すべての子を反復して正確な xml を出力するにはどうすればよいですか。
私の場合、印刷も機能せず、Noneが表示されます。
私は私の出力がこれに嘘をつくことを望みます:
<users>
<user username="admin" fullname="admin" password="admin" create_private_share="yes" remote_access="yes" grant_admin_rights="yes" enable_encrypt_share="yes" volume="DataVolume5" uid="1000"></user>
<user username="os" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
<user username="us" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
</users>
前もって感謝します :)