On Jan 19, 2005, at 03:58, David Rock wrote:
Indeed. The problem is, even if I know what I'm looking for, the problem remains that given the following document,
<foo> <bar>baz</bar> </foo>
If I want to get "baz", the command is ...
I'll try to find the time to write up a full example using ElementTree, Amara and dom4j. Meanwhile see http://www.oreillynet.com/pub/wlg/6225 and http://www.oreillynet.com/pub/wlg/6239
OK, here is code to print 'baz' from a simple XML string using three different XML toolkits. (I added another level to the XML to make it a little more challenging.)
This is pretty much a tie - it's three lines of code in each toolkit. The main difference is between the XPath access used by ElementTree and dom4j and the attribute access used by amara. Personally I find dom4j's full XPath support to be very handy - it essentially gives you a query engine built in to your data model. But it is a matter of taste, and amara has XPath support also.
I put each example inside 'except ImportError' so I could have them all in one file - it's not something you would normally do. The ElementTree and amara examples are for CPython; the dom4j example is for Jython.
Of course you need the corresponding toolkit to be correctly installed...
Kent
docText = '''
<doc>
<foo>
<bar>baz</bar>
</foo>
</doc>
'''# ElementTree
try:
from elementtree import ElementTreedoc = ElementTree.XML(docText)
# Note: doc represents the top-level ('doc') element
print 'ElementTree'
print doc.findtext('foo/bar')except ImportError:
print 'No ElementTree'# amara
try:
from amara import binderytoolsroot = binderytools.bind_string(docText)
# root is the 'root' element - the parent of the 'doc' element
print 'amara'
print root.doc.foo.barexcept ImportError:
print 'No amara'# dom4j
try:
import org.dom4j as domroot = dom.DocumentHelper.parseText(docText)
print 'dom4j'
print root.valueOf('doc/foo/bar')except ImportError:
print 'No dom4j'
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
