School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,404 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,613 people online right now. Registration is fast and FREE... Join Now!




Problem creating an XML DOM in a Python for loop

 

Problem creating an XML DOM in a Python for loop

baernolth1

25 Jun, 2009 - 08:14 AM
Post #1

New D.I.C Head
*

Joined: 25 Jun, 2009
Posts: 2

I've been writing some code to generate a XML document using the xml.dom.minidom module in Python. When I test my code in the Python shell by adding each entry in a dictionary manually it works and will perform the "print outfile.toxml()" function just fine. However, when I construct the document using a for loop it returns an error. Here is an example of the code:

CODE
outfile = xml.dom.minidom.Document() #create the xml file
    list = outfile.createElement("List")#create the list that is the root
    outfile.appendChild(list)               #of the file
    
    randomDict = {'asdf':6,'qewrty':42}
    for entry in randomDict.items():
        indentifier = outfile.createElement(entry[0])
        list.appendChild(identifier)
        answer = outfile.createElement(entry[1])
        identifier.appendChild(answer)

    print outfile.toxml()


Any ideas or suggestions would be helpful. Thanks!

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Problem Creating An XML DOM In A Python For Loop

25 Jun, 2009 - 08:22 AM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
For starters what's the error it's returning?
User is offlineProfile CardPM
+Quote Post

baernolth1

RE: Problem Creating An XML DOM In A Python For Loop

25 Jun, 2009 - 09:48 AM
Post #3

New D.I.C Head
*

Joined: 25 Jun, 2009
Posts: 2

QUOTE(PsychoCoder @ 25 Jun, 2009 - 08:22 AM) *

For starters what's the error it's returning?

Sorry about that PsychoCoder. Here is the error message I receive:

File "<pyshell#13>", line 1, in <module>
print outfile.toxml()
File "C:\Python26\lib\xml\dom\minidom.py", line 45, in toxml
return self.toprettyxml("", "", encoding)
File "C:\Python26\lib\xml\dom\minidom.py", line 57, in toprettyxml
self.writexml(writer, "", indent, newl, encoding)
File "C:\Python26\lib\xml\dom\minidom.py", line 1749, in writexml
node.writexml(writer, indent, addindent, newl)
File "C:\Python26\lib\xml\dom\minidom.py", line 817, in writexml
node.writexml(writer,indent+addindent,addindent,newl)
File "C:\Python26\lib\xml\dom\minidom.py", line 817, in writexml
node.writexml(writer,indent+addindent,addindent,newl)
File "C:\Python26\lib\xml\dom\minidom.py", line 804, in writexml
writer.write(indent+"<" + self.tagName)
TypeError: cannot concatenate 'str' and 'int' objects

Also the instance in the code where it defines "identifier" was misspelled.
Again, any help/suggestions would be great.
Thanks!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:34PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month