XPath based Python dictionaries, on loan
So Kimbro Staken posted this nifty idea to build XPath based Python dictionaries to access XML data as a part of his incredibly nifty Syncato microcontent management system. Eventually, I've really got to break down and get that thing built and running on my server and my laptop-- it really seems like I'm reinventing so many wheels by not basing dbagg3
on it.
But, while I'm in the process of wheel reinvention, how about I borrow Kimbro's idea? I just threw together a quick class called XPathDict, based on libxml2. It works a little something like this:
feed_xd = XPathDict(file="sample-atom.xml")
for entry_node in feed_xd.nodes("//atom:entry"):
entry = XPathDict(doc=entry_node.doc, node=entry_node)
print "Title: " % entry['atom:title']
if 'atom:author' in entry:
print "Author: " % entry['atom:author/atom:name']
xml = """
<dbagg3:user xmlns="http://purl.org/atom/ns#"
xmlns:dbagg3="http://decafbad.com/2004/07/dbagg3/">
<name>deusx</name>
<email>deus_x@pobox.com</email>
<url>http://www.decafbad.com/</url>
<dbagg3:prefs>
<dbagg3:pref name="foo">bar</dbagg3:pref>
</dbagg3:prefs>
</dbagg3:author>
"""
map = (
('userName', 'a:name'),
('userEmail', 'a:email'),
('fooPref', "dbagg3:prefs/dbagg3:pref[@name='foo']")
)
xd = XPathDict(xml=xml)
xd.cd("/dbagg3:user")
print xd.extract(map)
# {'userName' : 'deusx',
# 'userEmail' : 'deus_x@pobox.com',
# 'fooPref' : 'bar'}
There isn't any spectacular code behind all this, and the idea was Kimbro's, but it's working. It's also incredibly convenient, especially with the little XML-to-dict extraction map method I whipped up. This would take a bit more work to pry it out of its current context, such as turning the hardcoded namespaces into an option, among other things. But, here's the code for you to peruse.
(I got hooked early on subverting in-built language constructs from perl's tie
facilities, and C++'s operator overloading. Now I'm loving Python's special class methods. Someday, maybe, I'll actually get down to doing some work in LISP and wrap my head around some real language subversion.)
Anyway, while this is neither quite Native XML Scripting nor XML as a native language construct, it's getting there.
Archived Comments