Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:27:26+00:00 2026-05-26T20:27:26+00:00

I am reading an xml file and want to perform string operations on the

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:27:27+00:00Added an answer on May 26, 2026 at 8:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading an xml file and want to make it from a relative directory
My asp.net web application is reading and decrypting the password from a XML file
I want to access my sitemap.xml file in /mywebsite/sitemaps/xml/sitemap.xml Reading some documentation, it is
I want to create a xml file from the data given by the users.
i have created an xml file from my c# application i want to use
I want to load data from an XML file and assign that data to
I am having a xml file and i am reading the data from that
I'm reading XML file using Etree module. Im using following code to print the
I have a problem while reading from XML file in action script 3. This
I am reading an XML file into a DataSet and need to get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.