The _ElementInterface instance does not have the 'tostring' attribute

The code below generates this error. I can’t understand why. If ElementTree has parsing, why doesn't it have tostring? http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree

from xml.etree.ElementTree import ElementTree ... tree = ElementTree() node = ElementTree() node = tree.parse(open("my_xml.xml")) text = node.tostring() 
+4
source share
3 answers

tostring is a module method xml.etree.ElementTree , not a class with the same name 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) 
+8
source

tostring () is actually a function of the ElementTree module, not a method of the ElementTree wrapper class.

 >>> 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>' 
+4
source

The documents you are associated with do not support the existence of the ElementTree.tostring() method.

In addition, your call to tree.parse() will re-validate the node .

-1
source

Source: https://habr.com/ru/post/1301927/


All Articles