I've added a capability to the to_etree support that enables you to associate gDS instances with their corresponding etree elements. In order to use this, pass in a dictionary as the value of the keyword parameter mapping_. Here is an example::
mapping = {} rootElement1 = rootObj.to_etree(None, name_=rootTag, mapping_=mapping) Remember that you will need to use the --export command line option to generate the to_etree support. For example: $ generateDS.py --export="write etree" -o mylib.py ... The keys inserted into the dictionary are the instances of the gDS generated classes. The value associated with each key is etree element created from that instance. And, there is a convenience method, gds_reverse_node_mapping, that will reverse the order of the mapping. It produces a dictionary that maps in the reverse direction, for example, given a mapping from gDS instances to etree elements, it produces a mapping from etree elements to gDS instances. This capability is *not* yet in the released version at SourceForge.net and pypi.python.org/pypi. If you want to try it you will need to get a copy of the repository at Bitbucket: https://bitbucket.org/dkuhlman/generateds. Either use Mercurial or look for the "download" link to get a .zip file. Thanks again to Logan Owen for this capability. I'll have to admit, when he first suggested it, I asked myself: What could I possibly want that for? Then an hour later (I'm slow, I admit), I had one of those "Damn. I wish I'd thought of that." moments. Exporting to Lxml etree elements enables the ability to use a somewhat different style to explore the structured data from an XML document. These examples use the xpath capability provided by Lxml. In particular, you may want to learn about the abbreviated syntax of the XPath language: http://www.w3.org/TR/2010/REC-xpath20-20101214 http://www.w3.org/TR/2010/REC-xpath20-20101214/#abbrev http://www.w3.org/TR/2010/REC-xpath20-20101214/#unabbrev An example -- the following method could be added to a generated class -- It uses its associated etree element to do an xpath search of its descendents, then finds the gDS instances associated with each of those descendents, and then performs some operation on each one:: def sample_search(self, mapping, reverse_mapping): element1 = mapping[self] target_elements = element1.xpath('.//targetTag') for target_element in target_elements: target_obj = reverse_mapping[target_element] target_obj.sample_process() And, you might call the above method like this:: mapping = {} rootElement1 = rootObj.to_etree(None, name_=rootTag, mapping_=mapping) reverse_mapping = rootElement.gds_reverse_node_mapping(mapping) o o o some_object.sample_search(mapping, reverse_mapping) A slightly more extensive example -- Call this from one of the generated parseXX functions. This example uses the people.xml file in Demos/People:: def test01(rootObj, rootTag): mapping = {} rootElement = rootObj.to_etree(None, name_=rootTag, mapping_=mapping) reverse_mapping = rootObj.gds_reverse_node_mapping(mapping) # Find the person object whose name is "Bernardo". elements = rootElement.xpath('.//person/name[text() = "Bernardo"]/..') if elements: element = elements[0] print 'Bernardo\'s vehicle objects:' # Find the vehicles in the agents in person object Bernardo. vehicle_elements = element.xpath('./agent/vehicle') for vehicle_element in vehicle_elements: vehicle_obj = reverse_mapping[vehicle_element] print ' obj: %s wheel count: %d' % ( vehicle_obj, vehicle_obj.get_wheelcount(), ) else: print 'Cannot find Bernardo' print 'All objects containing <firstname>:' # Find all the objects that contain a firstname element. elements = rootElement.xpath('.//firstname/..') for element in elements: if element in reverse_mapping: obj = reverse_mapping[element] print ' obj: %s firstname: "%s"' % (obj, obj.get_firstname(), ) else: print ' element %s not found in reverse_mapping' % (element, ) When I apply this to people.xml in Demos/People, I see the following:: $ python tmp15sup.py people.xml Bernardo's vehicle objects: obj: <__main__.airplane object at 0x11a4e10> wheel count: 36 obj: <__main__.automobile object at 0x11a4e50> wheel count: 4 obj: <__main__.vehicleType object at 0x11a4e90> wheel count: 48 All objects containing <firstname>: obj: <__main__.hot_agent object at 0x11a4d10> firstname: "Paula" obj: <__main__.agentType object at 0x11a4d90> firstname: "Darren" obj: <__main__.boosterType object at 0x99d450> firstname: "David" obj: <__main__.boosterType object at 0x99d4d0> firstname: "Edward" obj: <__main__.agentType object at 0x11a4f10> firstname: "Harry" obj: <__main__.agentType object at 0x11ac050> firstname: "Ernest" obj: <__main__.agentType object at 0x99d610> firstname: "Harvey" obj: <__main__.agentType object at 0x99d710> firstname: "Harvey" As always, please let me know about suggestions or problems. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ------------------------------------------------------------------------------ Everyone hates slow websites. So do we. Make your web apps faster with AppDynamics Download AppDynamics Lite for free today: http://p.sf.net/sfu/appdyn_d2d_feb _______________________________________________ generateds-users mailing list generateds-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/generateds-users