[Tutor] elementtree question

Albert-Jan Roskam fomcl at yahoo.com
Thu Nov 19 16:23:50 CET 2009


Hi,
 
I have an elementtree question that probably reflects my inexperience with xml processing (and/or with Python). The xml file is a stream of the Spss Clementine program. Each stream consists of, among others, nodes. Each nodes has properties, among which "tooltiptext" and "label". I want to replace the contents of "label" to "tooltiptext".
 
Below is what I've cooked up till now. Could anyone give me some pointers? Thanks a lot in advance!
 
from elementtree import ElementTree as ET
"""
Replace the empty text of the tooltipText tag with the text of the label tag
Relevant part of the tree: stream/diagram/nodes/node/properties
Within properties, the tooltiptext tag is listed before the label tag.
"""
in_tree = ET.ElementTree(file="d:/jib/test.xml")
parent_map = dict((c, p) for p in in_tree.getiterator() for c in p)
def xml_read(parent_map):
    for c, p in parent_map.iteritems():
        if p.tag == "properties" and c.tag == "label":
            yield c.text
##newnames = xml_read(parent_map)
##for newname in newnames:
##    print newname
 
def xml_write(parent_map, in_tree):
    newnames = xml_read(parent_map)
    for c, p in parent_map.iteritems():
        if p.tag == "properties" and c.tag == "toolTipText":
            for newname in newnames:
                print newname
                c.text = newname
    in_tree.write("d:/jib/out_xml.xml")
xml_write(parent_map, in_tree)


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/d211d1be/attachment-0001.htm>


More information about the Tutor mailing list