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

  • SEARCH
  • Home
  • 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 333843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:59:57+00:00 2026-05-12T09:59:57+00:00

I am using the cElementTree library to parse XML files in Python. Everything is

  • 0

I am using the cElementTree library to parse XML files in Python.
Everything is working fine

But I would like to provide full error messages for the user when a value in the XML is not correct.

For example, let’s suppose I have the following XML:

<A name="xxxx" href="yyyy"/>

and want to tell the user if the href attribute doesn’t exist or have a value that is not in a given list.

For the moment, I have something like

if elem.get("ref") not in myList:
    raise XMLException( elem, "the 'href' attribute is not valid or does not exist")

where my exception is caught somewhere.

But, in addition, I would like to display the line number of the XML element in the file. It seems that the cElementTree doesn’t store any information about the line numbers of the XML elements of the tree… 🙁

Question: Is there an equivalent XML library that is able to do that?
Or a way to have access to the position of an XML element in the XML file ?

Thanks

  • 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-12T09:59:57+00:00Added an answer on May 12, 2026 at 9:59 am

    The equivalent library that you should be using is lxml. lxml is a wrapper on very fast c libraries libxml2 and libxslt and is generally considered superior to the built in ones.

    It, luckly, tries to keep to the element tree api and extend it in lxml.etree.

    lxml.etree has an attribute sourceline for all elements which is just what you are after.

    So elem.sourceline above in the error message should work.

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

Sidebar

Ask A Question

Stats

  • Questions 202k
  • Answers 202k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Figured it out myself. It seems that Resharper was using… May 12, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer Answering my own question, this script can be very useful… May 12, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer Why don't you set the cache directory to something a… May 12, 2026 at 8:21 pm

Related Questions

I am having trouble using the attribute XPath Selector in ElementTree, which I should
I am using ElementTree to parse a XML file. In some fields, there will
My XML file looks like the following: <?xml version=1.0?> <ItemSearchResponse xmlns=http://webservices.amazon.com/AWSECommerceService/2008-08-19> <Items> <Item> <ItemAttributes>
I am using the Photoshop's javascript API to find the fonts in a given

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.