from xml.dom import minidom

f = open("data.xml", "w")

doc = minidom.Document()
doc.standalone = True
rootNode = doc.createElement("root")
doc.appendChild(rootNode)
bookNode = doc.createElement("Test")
bookNode.setAttribute("isbn", "34909023")
rootNode.appendChild(bookNode)
authorNode = doc.createElement("Testname")
userpin_node = doc.createElement("Userpin")
sopin_node = doc.createElement('Sopin')
looptimes_node = doc.createElement('Looptimes')
datalen_node = doc.createElement('Datalen')
bookNode.appendChild(authorNode)
bookNode.appendChild(userpin_node)
bookNode.appendChild(sopin_node)
bookNode.appendChild(looptimes_node)
bookNode.appendChild(datalen_node)
authorTextNode = doc.createTextNode("101")
authorNode.appendChild(authorTextNode)
userpin_text_node = doc.createTextNode('123456')
userpin_node.appendChild(userpin_text_node)
sopin_text_node = doc.createTextNode('12345678')
sopin_node.appendChild(sopin_text_node)
looptimes_text_node = doc.createTextNode('50')
looptimes_node.appendChild(looptimes_text_node)
datalen_text_node = doc.createTextNode('32')
datalen_node.appendChild(datalen_text_node)
# doc.writexml(f, "/t", "/t", "/n", "utf-8")
bookNode.toprettyxml(encoding="utf-8")
doc.writexml(f, encoding="utf-8")

f.close()
