Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I've an xml file to be parsed. I did this using minidom parser of python. I had to add an attribute to a particular element which I did after the parsing. Now, I want to write the file back. I am not able to do this.

Below is the error trace which I get for this. I installed numpy 1.8 (win 32 python 2.7) version today. However, I had never tried writing xml file before. Could you please help?

Here's the code snippet:

xmlfile=open(xmlFile,'r')
xmldoc = minidom.parse(xmlFile)
tElements = xmldoc.getElementsByTagName("TEA")
for t in tElements:
    if(t.childNodes):
        print t.nodeType
        dataList = t.childNodes[0].data
        for data, csvData in product(dataList, clusterDataList):
            if(data == csvData[1]):
              t.setAttribute("cluster",csvData[0])
xmlfile.close()
fileWriter=open("sujatha_new.xml",'w')

xmldoc.writexml(fileWriter)
fileWriter.close()

Here's the exception:

Traceback (most recent call last):
  File "C:\Users\w44ylqrl\workspace\Python\Test\T\XMLConverter.py", line 215, in <module>
    addClusterInfo('..\\T\\preprocessed_For_Clustering\\outputs\\sujatha-new.csv', '..\\T\\xml\\sujatha.xml')
  File "C:\Users\w44ylqrl\workspace\Python\Test\T\XMLConverter.py", line 48, in addClusterInfo
    xmldoc.writexml(fileWriter)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 1752, in writexml
    node.writexml(writer, indent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 817, in writexml
    node.writexml(writer, indent+addindent, addindent, newl)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 807, in writexml
    _write_data(writer, attrs[a_name].value)
  File "C:\Python27\Lib\xml\dom\minidom.py", line 296, in _write_data
    data = data.replace("&", "&amp;").replace("<", "&lt;"). \
AttributeError: 'numpy.int64' object has no attribute 'replace'
share|improve this question
    
Please provide the code raising your error. –  alko Jan 9 '14 at 11:12
    
Hello alko, have added the code which causes this. Could you please take a look? –  user1930402 Jan 9 '14 at 11:29
1  
try adding string converting your data to strings before added it to the DOM –  catchmeifyoutry Jan 9 '14 at 11:33
    
Thank you catchmeifyoutry & alko for helping out :) –  user1930402 Jan 9 '14 at 14:29

1 Answer 1

up vote 0 down vote accepted

None of the python XML implementations allow you to serialize non-string representations, though some of them will let you assign them, if you want to abuse etree etc. as a treelike data structure. Some (like lxml) won't even let you assign them. Just make it a string:

t.setAttribute("cluster",str(csvData[0]))

and it will work. If you're deserializing these as well, you'll need to convert values back to int after loading.

share|improve this answer
    
Thanks a tonne!!!!It worked!! :) –  user1930402 Jan 9 '14 at 14:28
    
one additional thing i just caught... i'm not sure what exactly t.childnodes[0].data is, but it's also a string, not likely to be what you expected; at least, doing for x in data is going to iterate over the characters in string, not values; you might need to do a data.split(',') to make it a list, for this to really work as expected. –  Corley Brigman Jan 9 '14 at 14:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.