views:

193

answers:

1

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

+1  A: 

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.

Epcylon
XPath Support in ElementTree: http://effbot.org/zone/element-xpath.htm
Krab
I'm looking here: http://bit.ly/baRkvP, and I don't see a listing of the different XML DOM implementations. Can you point me to one or more list(s) of the available xml.dom pkgs (and hopefully include some links to examples)?
Sean Ochoa
The next two chapters in the manual you linked to covers `xml.dom.minidom`[1] and `xml.dom.pulldom`[2]. I can't think of any examples at the moment, but it should be fairly natural if you're used to working with DOM manipulation. That said, I recommend ElementTree if it can satisfy your needs. [1]: http://docs.python.org/library/xml.dom.minidom.html [2]: http://docs.python.org/library/xml.dom.pulldom.html
Epcylon