| |||||
XPath based Python Dictionaries
In my "secret" efforts to finally make some real progress on Syncato I just added a pretty cool feature to the Syncato XMLFragment API that allows you to treat an XML document sort of like a Python dictionary. Basically it allows you to use an XPath in place of the usual string key. This includes both reading and modifying the document. These definitely won't be interchangeable with Python dictionaries so call it an experiment if you must. My interest here isn't in being pythonic, it's in having maximum power and simplicity for using XML as a core application data structure. Given how the Syncato internals are written this provides a very valuable tool. It looks something like this.
doc = """<item>
<title>Test Post 1</title>
<description>Just a simple post to get started.</description>
<category>XML</category>
<category>Java</category>
</item>"""
fragment = XMLFragment(doc)
print "title " + fragment["/item/title"]
fragment["/item/title"] = "New title"
print "title " + fragment["/item/title"]
del fragment["/item/title"]
print "title " + fragment["/item/title"]
print "Category " + fragment["/item/category[1]"]
print "Category " + fragment["/item/category[2]"]
print "title " + fragment["/item/title[contains(., 'title')]"]
Running this you get output that looks something like this. title Test Post 1 title New title title Category XML Category Java Category XML The API also supports setting a base for a query so that if you always have the same beginning path you can make it even more convenient to work with the XML using the dictionary mechanisms.
doc = """<item>
<title>Test Post 1</title>
<description>Just a simple post to get started.</description>
<category>XML</category>
<category>Java</category>
</item>"""
fragment = XMLFragment(doc)
fragment.setBase("/item/")
print "title " + fragment["title"]
fragment["title"] = "New title"
print "title " + fragment["title"]
del fragment["title"]
print "title " + fragment["title"]
print "Category " + fragment["category[1]"]
print "Category " + fragment["category[2]"]
print "title " + fragment["title[contains(., 'title')]"]
Posted by Kimbro Staken Saturday Jul 17, 2004 at 6:09 PM | Recommended Sites JumpBox Virtual Appliances Virtualization Daily Grid7 Venturecast Inspirational Technology Scrollin on Dubs MC Ping - Microcontent Notfications
Archives
XML --
subscribe
Music -- subscribe Programming -- subscribe Python -- subscribe Syncato -- subscribe Photography -- subscribe Mac OS X -- subscribe General -- subscribe Canon EOS 1D -- subscribe Canon EOS 10D -- subscribe Canon EOS Digital Rebel -- subscribe Samsung Digimax V50 -- subscribe | ||||
Copyright 2002, 2003 Kimbro Staken
Powered By: Syncato 0.8 | |||||