0
我目前正在嘗試在Python中使用minidom創建一個雙父元XML文件,但是我努力讓它工作(並且掙扎着我意味着它不是)使用minidom(多個父元素)在Python中創建XML文件
我試圖創造一些這樣的:
<?xml version="1.0"?>
<twitter>
<account>
<name>Triple J</name>
<handle>triplejplays</handle>
<format>.{artist} - {title} [{time}]</format>
</account>
<account>
<name>BBC Radio 1</name>
<handle>BBCR1MusicBot</handle>
<format>Now Playing {artist} - {title}</format>
</account>
</twitter>
使用此代碼:
def createXML():
#Define document
xmlFile = Document()
#Create base element
baseElement = xmlFile.createElement("twitter")
#Create account element
accountElement = xmlFile.createElement("account")
#Append account element to base element
baseElement.appendChild(accountElement)
#Create elements and content under account
nameElement = xmlFile.createElement("name")
nameContent = xmlFile.createTextNode("Triple J")
nameContent.appendChild(nameElement)
nameElement.appendChild(accountElement)
handleElement = xmlFile.createElement("handle")
handleContent = xmlFile.createTextNode("triplejplays")
handleContent.appendChild(handleElement)
handleElement.appendChild(accountElement)
formatElement = xmlFile.createElement("format")
formatContent = xmlFile.createTextNode(".{artist} - {title} [{time}]")
formatContent.appendChild(formatElement)
formatElement.appendChild(formatElement)
print(doc.toxml(encoding='utf-8'))
createXML()
但我得到這個錯誤:
Text nodes cannot have children
有什麼辦法可以使這項工作?提前致謝!