I am reading an xml file and want to perform string operations on the node’s content.
import os
import elementtree.ElementTree as ET
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import tostring
xml_file = os.path.abspath(__file__)
xml_file = os.path.dirname(xml_file)
xml_file = os.path.join(xml_file, "Small1Review.xml")
print xml_file
root = ET.parse(xml_file).getroot()
text = tostring(root)
#print text
for a in text:
#print a, "-->", a.text
text = tostring(a)
print text
But the code gives the following error,
Traceback (most recent call last):
File "myEtXML.py", line 33, in <module>
text = tostring(a)
File "C:\Python26\lib\xml\etree\ElementTree.py", line 1009, in tostring
ElementTree(element).write(file, encoding)
File "C:\Python26\lib\xml\etree\ElementTree.py", line 543, in __init__
assert element is None or iselement(element)
AssertionError
How can I parse each node and perform some string operations on each of them.?
You’ve written
for a in text, buttextis a string and you’re treating it like an XML node.The
tostringmethod takes anetree.Element, but in this caseais a character of your stringtext.If you want to iterate over the tree, just treat it as a list
Also, your comment
#print a, "-->", a.textseems to indicate that you want thetextattribute of your nodes. This is not what’s returned by thetostringmethod. Thetostringmethod takes a node and makes an XML style string out of it. If you want the text attribute, just usea.text.