xml application advice

Diez B. Roggisch deets at nospam.web.de
Wed Jun 10 16:49:20 CEST 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 (at
> least that is how I think I want to do it).
> 
> An important part of this calculation is finding the 'hydraulically
> most remote' sprinkler.  This is something that I could specify with
> an attribute for now and later think about how to automate it.  I
> need to walk through the dom tree until I find a node of type
> "sprinkler" that has an attribute of hydraulically_most_remote with
> a value of True.
> 
> After I find this I need to break the itterator/for loop and then
> start walking backwards keeping a running total of the pressure drop
> until I reach a node that has multiple pipesections and then walk to
> the end of each branch and calculate the pressure drop, and then add
> them to the branch that contained the hydraulically most remote
> sprinkler, and then move on, repeating this until I walk all the way
> back to the inflow node.
> 
> I am having trouble finding a decent python/xml resource on the web.
> I have ordered Python & XML by Jones and Drake, but I am anxious to
> get something started.  The only decent online resource that I can
> seem to find is
> 
> http://pyxml.sourceforge.net/topics/howto/xml-howto.html
> 
> which doesn't seem to be a very comprehensive how-to.
> 
> Do demonstrate just about everything I know about xml and python I
> attached t.py and ex.xml.
> 
> Another thing that is confusing is dir(walker) does not show walker
> having an attribute currentNode and dir(walker.currentNode) does not
> show walker.currentNode having an attribute tagName.

Use lxml2 and xpath. 

http://codespeak.net/lxml/
http://codespeak.net/lxml/xpathxslt.html

See the below piece of code to get you started:

import lxml.etree as et

xml = """<?xml version="1.0"?>
<project name="test">
<inflow static="60" residual="20">
  <pipesection diameter="1.05" length="10">
    <node id="H1" k="5.6" type="sprinkler">
      <pipesection diameter="1.05" length="4">
         <node id="1" type="T">
           <pipesection diameter="1.05" length="6">
             <node id="H2" hydraulically_most_remote="True">
             </node>
           </pipesection>
           <pipesection diameter="1.05" length="5">
             <node id="H3">
             </node>
           </pipesection>
         </node>
      </pipesection>
    </node>
  </pipesection>
</inflow>
</project>"""



project = et.fromstring(xml)


hydraulically_most_remote =
project.xpath("//node[@hydraulically_most_remote='True']")[0]
print hydraulically_most_remote.attrib["id"]

# find node with multiple pipesections that's upwards

def find_mp_node(node):
    pipesections = node.xpath("pipesection")
    if len(pipesections) > 1:
        return node
    parent = node.getparent()
    if parent is not None:
        return find_mp_node(parent)

print find_mp_node(hydraulically_most_remote).attrib["id"]






More information about the Python-list mailing list