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

skaffman
  • 398,947
  • 96
  • 818
  • 769
bitcycle
  • 7,632
  • 16
  • 70
  • 121

1 Answers1

1

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
  • 4,674
  • 2
  • 26
  • 35
  • 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)? – bitcycle Feb 28 '10 at 19:41
  • 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 Mar 03 '10 at 09:22