2 Replies - 2779 Views - Last Post: 25 June 2009 - 10:48 AM Rate Topic: -----

#1 baernolth1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-June 09

Problem creating an XML DOM in a Python for loop

Posted 25 June 2009 - 09:14 AM

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:

 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!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem creating an XML DOM in a Python for loop

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Problem creating an XML DOM in a Python for loop

Posted 25 June 2009 - 09:22 AM

For starters what's the error it's returning?
Was This Post Helpful? 0
  • +
  • -

#3 baernolth1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-June 09

Re: Problem creating an XML DOM in a Python for loop

Posted 25 June 2009 - 10:48 AM

View PostPsychoCoder, on 25 Jun, 2009 - 08:22 AM, said:

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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1