Create XML file in Python using minidom (more than one parented element) -


i'm attempting create double-parented xml file within python using minidom, i'm struggling work (and struggling mean it's not)

i'm trying create this:

<?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> 

using code:

def createxml():     #define document     xmlfile = document()      #create base element     baseelement = xmlfile.createelement("twitter")      #create account element     accountelement = xmlfile.createelement("account")      #append account element base element     baseelement.appendchild(accountelement)      #create elements , 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() 

but error:

text nodes cannot have children 

is there way make work? in advance!

instead of e.g. namecontent.appendchild(nameelement) need e.g nameelement.appendchild(namecontent) need append text node create element node created earlier.


Comments