xml application advice

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jun 10 12:23:10 EDT 2009


William Purcell wrote:
> I am writing a application to calculate pressure drop for a piping
> network.  Namely a building sprinkler system.  This will be a
> command line program at first with the system described in xml....

If you are going to be doing a lot of tree walking, try etree.
Simple example:

     import xml.etree.ElementTree as ET # or wherever you get ElementTree

     def find_remote_and_path(node, path):
         for child in node:
             for result in walks(child, path + [node]): yield result
         if node.tag == 'node' and node.get('hydraulically_most_remote'
                                            ) == 'True':
             yield node, path


     tree = ET.parse('ex.xml')
     for node, path in find_remote_and_path(tree.getroot(), []):
         for t in path:
             print '    ', t.tag, t.get('id', '-')
         print node.tag, ', '.join(sorted('%s=%r' % pair
                                         for pair in node.items()))


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list