Parsing XML in Python using Expat
Background: I'm coming from C#-land, so I'm looking for something like being able to handle nodes and values by selecting via Xpath.
Here's my code, so far:
import urllib import sys from xml.parsers import expat url = 'http://SomeWebService.SomeDomain.com' u = urllib.urlopen(url) Parser = expat.ParserCreate() data = u.read() try: Parser.Parse(data) except: print "ERROR: Can't parse the XML" sys.exit(0)
What standard lib should I be using to deal with DOM elements as objects along with their attributes as one could in C#?
I'm looking for something like NodeList nodes = Parser.SelectNodes("Xpath")
I think you would have more luck if you tried using one of the xml.dom packages, or xml.etree.ElementTree. ElementTree has some limited xpath support, so if that's what you're used to, it might be the best choice.