2010-02-20 34 views

回答

8

tostringxml.etree.ElementTree模塊,而不是混淆的類似命名的類xml.etree.ElementTree.ElementTree的方法。

from xml.etree.ElementTree import ElementTree 
from xml.etree.ElementTree import tostring 

tree = ElementTree() 
node = tree.parse(open("my_xml.xml")) 
text = tostring(node) 
+0

此代碼有效。謝謝。 – Alex

-1

你掛不支持ElementTree.tostring()方法存在的文檔。

此外,您致電tree.parse()重新綁定node

4

tostring()實際上是ElementTree模塊的一個功能,而不是ElementTree包裝類的方法。

>>> import xml.etree.ElementTree as ET 
>>> x = ET.fromstring('<xml><one>one</one></xml>') 
>>> x  
<Element xml at 7f749572f710> 
>>> ET.tostring(x) 
'<xml><one>one</one></xml>' 
+0

我無法正常工作。我不斷收到''str'對象沒有屬性'iter'' – cxdf

相關問題