[Tutor] elementtree question

Gerard Flanagan grflanagan at gmail.com
Thu Nov 19 17:10:12 CET 2009


Albert-Jan Roskam wrote:
> 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)
>
>

That looks a bit over-thought. If I've understood what you want, 
something like below should do the job. It replaces the value of the id 
attribute with the value of the colour attribute.

ATTR_TEST_STRING = '''
<root>
    <title lang="en" encoding="utf-8">Document Title</title>
    <category id="123" code="A">
        <item id="A001" colour="red">Item A1</item>
        <item id="A002" colour="blue">Item A2</item>
        <item id="A003" colour="yellow">Item A3</item>
    </category>
    <category id="456" code="B">
        <item id="B001" colour="pink">Item B1</item>
        <item id="B002" colour="blue">Item B2</item>
        <item id="B003" >Item B3</item>
    </category>
    <category id="789" code="C">
        <item id="C001" colour="pink">Item C1</item>
        <item id="C002" colour="orange">Item C2</item>
        <item id="C003" colour="blue">Item C3</item>
    </category>
</root>'''

from xml.etree import ElementTree as ET

xml = ET.fromstring(ATTR_TEST_STRING)

print('-------- before -------')
ET.dump(xml)

for elem in xml.getiterator():
    if 'id' in elem.attrib and 'colour' in elem.attrib:
        elem.set('id', elem.get('colour', ''))

print('-------- after -------')
ET.dump(xml)




More information about the Tutor mailing list