xml element tree to html problem

Ron Adam rrr at ronadam.com
Wed Apr 5 00:02:48 EDT 2006


Fredrik Lundh wrote:

> here's one way to do it:
> 
>     import cElementTree as ET
> 
>     tree = ET.XML("""
>     <object>
>         <name>ball</name>
>         <desc>
>             <color>red</color>
>             <size>large</size>
>         </desc>
>     </object>
>     """)
> 
>     MAP = {
>         "object": ("dl", "object"),
>         "name":   ("dt", "name"),
>         "desc":   ("ul", None),
>         "color":  ("li", "color"),
>         "size":   ("li", "size"),
>     }
> 
>     for elem in tree.getiterator():
>         elem.tag, klass = MAP[elem.tag]
>         if klass:
>             elem.set("class", klass)
> 
>     print ET.tostring(tree)


Thanks a *LOT!*  :-)

This is what I needed.  Now I can play with finding the best data 
structure along with what elements to translate each tag to.

This is for a rewrite of PyDoc.py.  I'm hoping it will be as easy to 
write to other formats from the XML as it is to html.

Cheers,
    Ron



More information about the Python-list mailing list