xml element tree to html problem

Discussion in 'Python' started by Ron Adam, Apr 4, 2006.

  1. Ron Adam

    Ron Adam Guest

    I'm new to element tree and haven't been able to find a simple solution
    to this problem yet. So maybe someone can point me in the right direction.

    I have an element tree structure of nested elements that I want to
    convert to html as nested definition and unordered lists in the
    following way.

    <object>
    <name>ball</ball>
    <desc>
    <color>red</color>
    <size>large</size>
    </desc>
    </object>


    To...

    <dl class='object'>
    <dt class='name'>ball</dt>
    <dd class='desc'>
    <ul>
    <li class='color'>red</li>
    <li class='size'>large</li>
    </ul>
    </dd>
    </dl>


    Where each xml tag has a predefined relationship to either definition
    list or an unordered list html tag. 'object' is always mapped to <dl
    class='object'>, 'name' is always mapped to <dt class='name'>. etc...

    So I will probably have a dictionary to look them up. The problem I
    have is finding a relatively painless way to do the actual translation.

    Thanks in advance for any advise.

    Cheers,
    Ron
     
    Ron Adam, Apr 4, 2006
    #1
    1. Advertising

  2. Ron Adam wrote:

    > I have an element tree structure of nested elements that I want to
    > convert to html as nested definition and unordered lists in the
    > following way.
    >
    > <object>
    > <name>ball</ball>
    > <desc>
    > <color>red</color>
    > <size>large</size>
    > </desc>
    > </object>
    >
    >
    > To...
    >
    > <dl class='object'>
    > <dt class='name'>ball</dt>
    > <dd class='desc'>
    > <ul>
    > <li class='color'>red</li>
    > <li class='size'>large</li>
    > </ul>
    > </dd>
    > </dl>
    >
    >
    > Where each xml tag has a predefined relationship to either definition
    > list or an unordered list html tag. 'object' is always mapped to <dl
    > class='object'>, 'name' is always mapped to <dt class='name'>. etc...
    >
    > So I will probably have a dictionary to look them up. The problem I
    > have is finding a relatively painless way to do the actual translation.


    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)

    this prints:

    <dl class="object">
    <dt class="name">ball</dt>
    <ul>
    <li class="color">red</li>
    <li class="size">large</li>
    </ul>
    </dl>


    here's a somewhat simpler (but less general) version:

    MAP = dict(object="dl", name="dt", desc="ul", color="li", size="li")

    for elem in tree.getiterator():
    if elem.tag != "desc":
    elem.set("class", elem.tag)
    elem.tag = MAP[elem.tag]

    hope this helps!

    </F>
     
    Fredrik Lundh, Apr 4, 2006
    #2
    1. Advertising

  3. Ron Adam

    Guest

    are you using PyXML?
    If this is a simple one to one relationship with dependence on order,
    I'd forgo the whole PyXML, read the file line by line, and replace tags
    as appropriate. You may have to do some simple checkin, in case there
    is n <object> <name>object</name>


    Otherwise, have fun with the parsers - nothing is painless is SAX or
    DOM.

    as far as simple translation? once you have tokenized or used PyXML to
    get the elements, something like?

    for tag in listofTags:
    if tagDictionary.has_key(tag):
    doSomething(tagDictionary[tag])


    IMHO - PyXML is a great tool, but for something like simple
    substitution, it's so so much overkill. For simple processing tasks
    like this I almost always just treat it as a text file. I resort to
    PyXML when things like heirarchy are important. Hope this helps
     
    , Apr 4, 2006
    #3
  4. Ron Adam

    Guest

    Frederick,
    I didn't know about cElementTree before, wow - this is a lot nicer than
    PyyXML - and a whole lot faster. Almost makes my comments about
    dealing with the xml as text, completely pointless.
     
    , Apr 4, 2006
    #4
  5. Ron Adam

    Ron Adam Guest

    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
     
    Ron Adam, Apr 5, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joris Gillis
    Replies:
    2
    Views:
    1,566
    Joris Gillis
    Jun 16, 2006
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,234
  3. HANM
    Replies:
    2
    Views:
    762
    Joseph Kesselman
    Jan 29, 2008
  4. Peter Reimer
    Replies:
    4
    Views:
    1,121
    Peter Flynn
    Apr 12, 2010
  5. afrinspray
    Replies:
    2
    Views:
    146
    afrinspray
    Sep 7, 2006
Loading...

Share This Page