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")

Answers


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.


Need Your Help

ModelState.AddModelError is not being displayed inside my view

asp.net-mvc-3 razor

I have the following view,, which create 10 ajax.beginform ,, But the problem that i am facing is that incase an error occurs during the creation of the object then the ModelState.AddModelError wil...

How do I call a web service to get xml data displayed in a treeview in asp.net

asp.net xml web-services drop-down-menu treeview

I am trying to use a asp:dropdownlist control to be able to choose values from a local database I have created within my app_data folder. They are stock exchange symbols. When I select a symbol fro...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.