0

I'm currently attempting to create a double-parented XML file within Python using minidom, however I'm struggling to get it to work (and by struggling I mean it's not)

I'm trying to create something like 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 this code:

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()

But I get this error:

Text nodes cannot have children

Is there any way to make this work? Thanks in advance!

1 Answer 1

1

Instead of e.g. nameContent.appendChild(nameElement) you need e.g nameElement.appendChild(nameContent) as you need to append the text node create to the element node created earlier.

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.