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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:10+00:00 2026-06-15T15:48:10+00:00

I was trying to parse an xml file. My problem is same as this:

  • 0

I was trying to parse an xml file.
My problem is same as this:

parsing an xml file for unknown elements using python ElementTree

And I tried the solution of untubu.

It works great. But only for the lines which have single tags

For example:

   <some_root_name>
<tag_x>bubbles</tag_x>
 </some_root_name>

This works great
But if it is like:

src = '''\
<review type="review"><link>http://www.openlist.com/new-york-ny/mickey-mantles/27612417/?numReviews=178</link>
'''

it fails..
I have many instances like this.
I don’t want to go beyond native libraries usage because after this I will run the code on different computer (prod env) and I will have to set the libraries there.. and it gets messy..

Is there a way , i can modify the original solution to solve this out.
Thanks.

The code from above link:

import xml.sax as sax
import xml.sax.handler as saxhandler
import pprint

class TagParser(saxhandler.ContentHandler):
    # http://docs.python.org/library/xml.sax.handler.html#contenthandler-objects
    def __init__(self):
        self.tags = {}
    def startElement(self, name, attrs):
        self.tag = name
    def endElement(self, name):
        if self.tag:
            self.tags[self.tag] = self.data
            self.tag = None
            self.data = None
    def characters(self, content):
        self.data = content

parser = TagParser()
src = '''\
<some_root_name>
    <tag_x>bubbles</tag_x>
    <tag_y>car</tag_y>
    <tag...>42</tag...>
</some_root_name>'''
sax.parseString(src, parser)
pprint.pprint(parser.tags)

Exception trace:

File "extract_xml.py", line 59, in unittest
  sax.parseString(src, parser)
File "C:\Python27\lib\xml\sax\__init__.py", line 49, in parseString
  parser.parse(inpsrc)
File "C:\Python27\lib\xml\sax\expatreader.py", line 107, in parse
  xmlreader.IncrementalParser.parse(self, source)
File "C:\Python27\lib\xml\sax\xmlreader.py", line 125, in parse
  self.close()
File "C:\Python27\lib\xml\sax\expatreader.py", line 217, in close
  self.feed("", isFinal = 1)
File "C:\Python27\lib\xml\sax\expatreader.py", line 211, in feed
  self._err_handler.fatalError(exc)
File "C:\Python27\lib\xml\sax\handler.py", line 38, in fatalError
  raise exception
xml.sax._exceptions.SAXParseException: <unknown>:2:4: no element found
  • 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-15T15:48:12+00:00Added an answer on June 15, 2026 at 3:48 pm

    The TagParser uses endElement to add data to self.tags.

    With src equal to

    src = '''\
    <review type="review"><link>http://www.openlist.com/new-york-ny/mickey-mantles/27612417/?numReviews=178</link></review>
    '''
    

    The <review> has no closing tag, </review>, so endElement never gets called.

    If you add a closing </review> tag to src:

    src = '''\
    <review type="review"><link>http://www.openlist.com/new-york-ny/mickey-mantles/27612417/?numReviews=178</link></review>
    '''
    

    then the program yields

    {u'link': u'http://www.openlist.com/new-york-ny/mickey-mantles/27612417/?numReviews=178'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse an XML file with python using lxml, but get
I'm trying to parse an xml file with PHP. I'm using this code and
I am beginner in PHP. I am trying to parse this xml file. <relationship>
I am trying to parse an XML file using the SAX interface of libxml2
I am trying to parse this XML file for a school project: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/genre=20/xml .
I'm trying to parse a moderately large XML file (6mb) in php using simpleXML.
I'm trying to parse an xml file that contains accents, but I get this
I want to parse an XML file from URL using JDOM. But when trying
I have been trying to parse a file with xml.etree.ElementTree : import xml.etree.ElementTree as
I have an XML file that I am trying to parse with Sax (this

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.