So, in my great big XML library, I wanted to build a query-like interface for the QA Automaters to be able to use that would abstract out a lot of the grunt work of getting the XML object, setting up the Elementtree object and doing the basic parsing we need. What I settled on was a 3 Module approach:
GetXML: grabs the XML from the IP/URL, returns an ElementTree.fromstring() object BasicParse: Extends GetXML, does the basic parsing and some looping of the GetXML calls for the XML
The key was the third module I wanted to build: QueryInterface - I wanted to use XPath syntax to be able to do targetted searches within the XML tree and allow users to call a series of pre-built queries (with the ability to pass in custom queries).
ElementTree has very rudimentary XPath support - the real savior in this project was an ElementTree addon called ElementFilter. ElementFilter is a wrapper that allows you to "Find or remove nodes from ElementTree XML using an XPath-like filter."
It's a perfect fit. All of the methods in the QueryInterface class are super simple:
def getNodeStatus(self): """ Returns a list of objects of all node/status inside the tree """ path = r"node/status[@stateString=='AVAIL']/" return findall(self.tree, path)
It really made life easier for me. For custom queries I can allow people to pass through the r"%s" % (query) section:
def customQuery(self, query): """ Expects: an xpath-like query in format Returns: A list of objects containing all results for the query. """ path = r"%s" % (query) return findall(self.tree, path)
Viola! If I were you, go check out ElementFilter