0

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.?

1 Answer 1

2

You've written for a in text, but text is a string and you're treating it like an XML node.

The tostring method takes an etree.Element, but in this case a is a character of your string text.

If you want to iterate over the tree, just treat it as a list

root = ET.parse(xml_file).getroot()
for child in root:
    print tostring(child)

Also, your comment #print a, "-->", a.text seems to indicate that you want the text attribute of your nodes. This is not what's returned by the tostring method. The tostring method takes a node and makes an XML style string out of it. If you want the text attribute, just use a.text.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.