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 8720749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:06:19+00:00 2026-06-13T07:06:19+00:00

i am reading an xml file using xpath the issue its returning null. main

  • 0

i am reading an xml file using xpath the issue its returning null.

main.py

#!/usr/bin/python
import sys
from util import xml_util

def main():

    try:
        xml = XMLReader('test.xml')
        x = xml.getitem('numbers/x')
    print x
        return 0
    except:
        return -1


if __name__ == '__main__':
    sys.exit(main())

xml_util.py

from lxml import etree

class XMLReader:
    "Parse file and read data using find(path)"
    def __init__(self,fname):
        self.tree = etree.parse(fname)
        self.root = self.tree.getroot()

    def getitem(self,path,converter=str):
        return converter(self.root.find(path).text)

    def getlist(self,path,converter=str):
        return [converter(item.text) for item in self.root.find(path)]

test.xml

<mystuff>
   <numbers>
        <x>3.14</x>
        <y>42</y>
        <z>11</z>
   </numbers>
</mystuff>

The XMLReader class is received from this post https://stackoverflow.com/questions/4357494/how-do-i-read-data-using-lxml-in-python

  • 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-06-13T07:06:20+00:00Added an answer on June 13, 2026 at 7:06 am
    #!/usr/bin/python
    import sys
    from xml_util import XMLReader
    
    def main():
    
        try:
            xml = XMLReader('test.xml')
            x = xml.getitem('numbers/x')
        print x
            return 0
        except:
            return -1
    
    
    if __name__ == '__main__':
        sys.exit(main())
    

    Edit

    If you’re still haveing problems and you set up a module with __init__.py and are trying to import xml_util then reference your reader xml_util.XMLReader or change your import statment to from util.xml_util import XMLReader

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

Sidebar

Related Questions

I am reading an xml file using javascript and then I need to submit
I'm reading an xml file using Jaxb, now the contents of the xml file
When reading a XML file with linq to XML using a XDocument and there
I'm using Qt C++ and am reading in an XML file for data. I
I am reading a xml file with: $reader = new XMLReader(); $reader->xml($myXml, NULL, LIBXML_NOWARNIG
what is the best way of reading xml file using linq and the below
I am reading an XML file using an XMLTextReader. Is there a way to
I am reading an xml file using php, after reading those files i am
I am studying Linq to xml and i am reading a xml file using
I am trying to create a dynamic menu by reading an XML file using

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.