effbot's ElementTree module (http://effbot.org/zone/element-index.htm) is an excellent way to work with XML. It has a lot of incarnations though so distributing code that uses it right now is little bit of a pain. Here is a snippet you might want to use to import one of all the available incarnations -- hopefully the users of your script will have one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | try:
import xml.etree.ElementTree as ET # in python >=2.5
except ImportError:
try:
import cElementTree as ET # effbot's C module
except ImportError:
try:
import elementtree.ElementTree as ET # effbot's pure Python module
except ImportError:
try:
import lxml.etree as ET # ElementTree API using libxml2
except ImportError:
import warnings
warnings.warn("could not import ElementTree "
"(http://effbot.org/zone/element-index.htm)")
# Or you might just want to raise an ImportError here.
# Use ET.Element, ET.ElementTree, etc...
|
Tags: xml